Reppo for internal functions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.2 KiB

  1. ---
  2. title: "CitFuns"
  3. output: rmarkdown::html_vignette
  4. description: |
  5. How to use CitFuns functions.
  6. vignette: >
  7. %\VignetteIndexEntry{CitFuns Package}
  8. %\VignetteEngine{knitr::rmarkdown}
  9. %\VignetteEncoding{UTF-8}
  10. ---
  11. ```{r setup, include=FALSE}
  12. knitr::opts_chunk$set(echo = TRUE, message = F, warning = F, error = F)
  13. ```
  14. ## Installation
  15. In order to install CitFuns package from Git reppository, you must install `devtools` package:
  16. ```{r, eval=F}
  17. install.packages('devtools')
  18. ```
  19. In order to install it, you will have to install (if not already done) [Rtools](https://cran.r-project.org/bin/windows/Rtools/rtools40.html).
  20. You will also need [Git](https://git-scm.com/downloads) installed in your computer.
  21. Now we are ready to install CitFuns package:
  22. ```{r, eval=F}
  23. devtools::install_git("https://git.ratg.cat/marcelcosta/CitFuns.git", build_vignettes = T)
  24. ```
  25. ## Update
  26. Any time you want to update the package, you must *reinstall* it:
  27. ```{r, eval=F}
  28. detach("package:CitFuns", unload = TRUE) # Only required if you have loaded the package in this session.
  29. devtools::install_git("https://git.ratg.cat/marcelcosta/CitFuns.git")
  30. ```
  31. ## ggheatmap
  32. **ggheatmap** generates ggplot heatmaps easily.
  33. We start by loading required packages, including *CitFuns*
  34. ```{r}
  35. library(tidyverse)
  36. library(CitFuns)
  37. ```
  38. Now we will create an example dataframe:
  39. ```{r}
  40. df<-data.frame("pats"=paste0("PAT", 1:20), "CytA"=rnorm(20,5), "CytB"=rnorm(20,5),
  41. "CytC"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytD"=rnorm(20,5),
  42. "CytE"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytF"=rnorm(20,5),
  43. "CytG"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)))
  44. df<-gather(df, Cyt, Value,-pats)
  45. head(df)
  46. ```
  47. Usually, this package works with the dataframes in **Long** format, as it is intended in ggplot workflow.
  48. And now we will generate the heatmap.
  49. ```{r, fig.width=8}
  50. ggheatmap(df)
  51. ```
  52. As we can observe, X and Y axis are sorted by cluster detection (using *hclust*).
  53. *ggheatmap* groups the results if more there is more than one observation for each X-Y coordinate. By default, it calculates the mean, but the median can also be used instead. To show this, we will create another variable, "Met", and we plot Cytokine expression *versus* Met status.
  54. ```{r, fig.width=8}
  55. clinics<-data.frame("pats"=df$pats %>% unique, "Met"=rep(c("0","1"), 10))
  56. df<-merge(df, clinics)
  57. ggheatmap(df, x="Cyt",y="Met", grouping = "median")
  58. ```
  59. By default, *other variables not used are eliminated* during the grouping process. However, if you want to further use them (for faceting, for example), you can use the *exclude_group* parameter to keep them in the data.frame.
  60. ```{r, fig.width=8}
  61. ggheatmap(df, exclude_group = "Met")+facet_grid(.~Met, scales = "free")
  62. ```
  63. Finally, you can scale the heatmap either by rows or by columns:
  64. ```{r, fig.width=8}
  65. ggheatmap(df, scale="rows")
  66. ```
  67. It is worth noticing that ggheatmap outputs a ggplot object, so you can further modify it as you are used to:
  68. ```{r, fig.width=8, fig.height=5}
  69. ggheatmap(df)+
  70. scale_fill_gradient(low = "black", high = "yellow")+
  71. scale_y_discrete(position = "right")+
  72. theme(legend.position = "bottom")
  73. ```
  74. ## ggcorrplot
  75. ggcorrplot generates a correlation matrix. Using the same example dataframe, you have to specify which variable and value columns will be used to test correlation.
  76. ```{r, fig.width=5, fig.height=4}
  77. ggcorrplot(df, var = "Cyt", value = "Value")
  78. ```
  79. You can specify a color for the tile lines, transparent by default:
  80. ```{r, fig.width=5, fig.height=4}
  81. ggcorrplot(df, var = "Cyt", value = "Value", color = "white")
  82. ```
  83. By default, ggcorrplot converts the p-value into *star* significance equivalence. You can show the pvalue or nothing ("none").
  84. ```{r, fig.width=5, fig.height=4}
  85. ggcorrplot(df, var = "Cyt", value = "Value", color = "white", stat="pval")
  86. ```
  87. ggcorrplot uses "pearson" by default to obtain a correlation coeficient, although "spearman" is also available.
  88. ```{r, fig.width=5, fig.height=4}
  89. ggcorrplot(df, var = "Cyt", value = "Value", color = "white", stat="pval", method="spearman")
  90. ```
  91. Finally, you can show only the upper part or the lower part of the specular matrix.
  92. ```{r, fig.width=5, fig.height=4}
  93. ggcorrplot(df, var = "Cyt", value = "Value", color = "black", tri="lower")
  94. ```