Compare commits
68 Commits
0f3f1f86a0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c88359c801 | |||
| a9bff2e058 | |||
| 217bca2480 | |||
| ab96f5ec6a | |||
| d42e4ff6f3 | |||
| 9ef27ea7de | |||
| a4483c84e6 | |||
| 0cb487caa4 | |||
| c1bd92a7c9 | |||
| cde38f8aa6 | |||
| 296d98706a | |||
| ed125951a8 | |||
| 389b97d3b4 | |||
| de89c5258d | |||
| f44ec4b7fe | |||
| d4521b874c | |||
| 9ab832c900 | |||
| e57141aa22 | |||
| bd98a08c31 | |||
| fb62bdf49d | |||
| 4445b00037 | |||
| 7025db93b6 | |||
| 7a9b49f859 | |||
| 3c77771ccb | |||
| 8fe1c77f74 | |||
| 9fe57a3f32 | |||
| 9c9d5e3a52 | |||
| 46b6efc706 | |||
| 728b2429f6 | |||
| 31811bec30 | |||
| 05da9ac572 | |||
| d49b1b2226 | |||
| 058115e4a8 | |||
| bbaa881479 | |||
| 6a0501eb71 | |||
| 69d5674814 | |||
| 171df0f3a9 | |||
| 06016990f7 | |||
| b39098533d | |||
| cb575ac297 | |||
| 62547fecd5 | |||
| a2d10b2308 | |||
| eeba7ea855 | |||
| 0b6f3b80d1 | |||
| 0794c9fc1e | |||
| f9dd553a8c | |||
| f9101f2351 | |||
| f6b014b0b8 | |||
| c7104d3903 | |||
| 01addb00e5 | |||
| 2a78288451 | |||
| 5d9fec1c62 | |||
| 4ed03c2e25 | |||
| 7c9e2b7f89 | |||
| 3e6ea2693c | |||
| 0281fa95c8 | |||
| d51fe8a64d | |||
| 6e6d01b3fc | |||
| 41d73a8396 | |||
| 2cd480bfd0 | |||
| d826de7da9 | |||
| 719637934c | |||
| 05ac7461b6 | |||
| 8e92d86ec8 | |||
| 4db08dc4f4 | |||
| 81e3902250 | |||
| 7d03719bf3 | |||
| ece3a80215 |
@@ -7,4 +7,6 @@ Maintainer: Marcel Costa <marcelcosta@mcosta.cat>
|
||||
Description: Funciones internas para el laboratorio CIT
|
||||
License: GPL
|
||||
Encoding: UTF-8
|
||||
Suggests: rmarkdown
|
||||
VignetteBuilder: knitr
|
||||
LazyData: true
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
ggcorrplot<-function(df, var, value,color="#FFFFFF00", stat="signif", tri="all", method="pearson"){
|
||||
allnames<-colnames(df)
|
||||
m.df<-df %>% spread(all_of(var), all_of(value)) %>% select(!any_of(allnames))
|
||||
mcor<-cor(m.df, m.df, use="pairwise.complete.obs", method = method) # Por defecto usa el método de Pearson.
|
||||
mpval<-Hmisc::rcorr(as.matrix(m.df), type=method)$P
|
||||
|
||||
order<- mcor %>% as.data.frame() %>% add_column(Var1=rownames(mcor),.before=1) %>% clustsort
|
||||
|
||||
mcor<-mcor[order$y, order$x]
|
||||
mpval<-mpval[order$y, order$x]
|
||||
|
||||
df<-mcor %>% as.data.frame()
|
||||
if (tri == "upper"){
|
||||
df[lower.tri(df, diag=F)]<-NA
|
||||
}
|
||||
if (tri == "lower"){
|
||||
df[upper.tri(df, diag=F)]<-NA
|
||||
}
|
||||
df<-df %>% add_column(Var1=rownames(mcor),.before=1) %>%
|
||||
gather(Var2, Value, -Var1) %>% filter(!is.na(Value))
|
||||
|
||||
df.pval<-mpval %>% as.data.frame()
|
||||
if (tri == "upper"){
|
||||
df.pval[lower.tri(df.pval, diag=F)]<-NA
|
||||
}
|
||||
if (tri == "lower"){
|
||||
df.pval[upper.tri(df.pval, diag=F)]<-NA
|
||||
}
|
||||
df.pval<-df.pval %>% add_column(Var1=rownames(mpval),.before=1) %>%
|
||||
gather(Var2, Value, -Var1) %>% filter(!is.na(Value))
|
||||
|
||||
df.pval$Value<-round(df.pval$Value, 3)
|
||||
if (!stat %in% c("signif","none","pval")){stat<-"signif"}
|
||||
if (stat=="signif"){
|
||||
df.pval$Value<-gtools::stars.pval(df.pval$Value)
|
||||
}
|
||||
if (stat=="none"){
|
||||
df.pval$Value<-""
|
||||
}
|
||||
|
||||
|
||||
df$Var1<-factor(df$Var1, levels=order$x)
|
||||
df$Var2<-factor(df$Var2, levels=order$y)
|
||||
|
||||
ggplot(df, aes(Var1, Var2))+
|
||||
geom_tile(aes(fill=Value), color=color)+
|
||||
geom_text(data=df.pval, aes(label=Value), color="white")+
|
||||
scale_fill_gradientn(colors=col2(200))+
|
||||
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5),
|
||||
panel.background = element_blank(),
|
||||
axis.ticks = element_blank())
|
||||
}
|
||||
+57
-7
@@ -1,23 +1,73 @@
|
||||
ggheatmap<-function(df, x=NULL, y=NULL, value=NULL, grouping="mean"){
|
||||
ggheatmap<-function(df, x=NULL, y=NULL, value=NULL, grouping="mean", exclude_group=NULL, scale="none",color="#FFFFFF00"){
|
||||
if (is.null(x)){x=colnames(df)[1]}
|
||||
if (is.null(y)){y=colnames(df)[2]}
|
||||
if (is.null(value)){value=colnames(df)[3]}
|
||||
|
||||
df<-rename(df, "VarX"=all_of(x), "VarY"=all_of(y), "Value"=all_of(value))
|
||||
|
||||
if (is.null(exclude_group)){
|
||||
df<-df %>% group_by(VarX,VarY)
|
||||
}else{
|
||||
df<-df %>% group_by_("VarX","VarY",exclude_group) #%>% rename(exclude_group="all_of(exclude_group)")
|
||||
}
|
||||
if (grouping == "mean"){
|
||||
df<-df %>% group_by(VarX,VarY) %>% summarise(Value=mean(Value))
|
||||
df<-df %>% summarise(Value=mean(Value)) %>% ungroup
|
||||
}
|
||||
if (grouping == "median"){
|
||||
df<-df %>% group_by(VarX,VarY) %>% summarise(Value=median(Value))
|
||||
df<-df %>% summarise(Value=median(Value)) %>% ungroup
|
||||
}
|
||||
|
||||
order<-clustsort(df %>% spread(VarY,Value) %>% as.data.frame)
|
||||
if (length(unique(df$VarX)) > 1 & length(unique(df$VarY)) >
|
||||
1) {
|
||||
order <- clustsort(df %>% spread(VarY, Value) %>% select(!all_of(exclude_group)) %>%
|
||||
as.data.frame)
|
||||
}
|
||||
else {
|
||||
order <- list()
|
||||
if (length(unique(df$VarX)) > 1) {
|
||||
xhclust <- df %>% spread(VarY, Value) %>%
|
||||
select(!all_of(exclude_group)) %>% as.data.frame
|
||||
order[["x"]] <- pull(xhclust, 1)[hclust(dist(xhclust %>%
|
||||
select(-1)))$order]
|
||||
}
|
||||
else {
|
||||
order[["x"]] <- df %>% pull(VarX) %>% unique
|
||||
}
|
||||
if (length(unique(df$VarY)) > 1) {
|
||||
yhclust <- clustsort(df %>% spread(VarY, Value) %>%
|
||||
select(!all_of(exclude_group)) %>% as.data.frame)
|
||||
order[["y"]] <- colnames(yhclust)[2:ncol(yhclust)][hclust(dist(t(yhclust %>%
|
||||
select(-1))))$order]
|
||||
}
|
||||
else {
|
||||
order[["y"]] <- df %>% pull(VarY) %>% unique
|
||||
}
|
||||
}
|
||||
|
||||
if (scale != "none"){
|
||||
if (scale == "rows"){
|
||||
cols<-unique(df$VarY)
|
||||
sca.df<-spread(df, VarY, Value)
|
||||
for (i in cols){sca.df[,i]<-scale(sca.df[,i])}
|
||||
df<-gather(sca.df, VarY, Value, all_of(cols))
|
||||
}
|
||||
if (scale == "cols"){
|
||||
cols<-unique(df$VarX)
|
||||
sca.df<-spread(df, VarX, Value)
|
||||
for (i in cols){sca.df[,i]<-scale(sca.df[,i])}
|
||||
df<-gather(sca.df, VarX, Value, all_of(cols))
|
||||
}
|
||||
}
|
||||
|
||||
df$VarX<-factor(df$VarX, levels=order$x)
|
||||
df$VarY<-factor(df$VarY, levels=order$y)
|
||||
|
||||
df %>%
|
||||
ggplot(aes(VarX, VarY, fill=Value))+
|
||||
scale_x_discrete(limits=order$x)+
|
||||
scale_y_discrete(limits=order$y)+
|
||||
labs(x=x, y=y)+
|
||||
theme_heatmap()
|
||||
geom_tile(aes(fill=Value), color=color)+
|
||||
scale_fill_gradientn(colors=col2(200))+
|
||||
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5),
|
||||
panel.background = element_blank(),
|
||||
axis.ticks = element_blank())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
gglegend<-function(data, x, y, var, stat="median", color="black", ...){
|
||||
if (stat == "median"){
|
||||
data<-data %>% group_by({{var}}) %>% summarise("{{x}}":=median({{x}}, na.rm=T), "{{y}}":=median({{y}}, na.rm=T))
|
||||
}
|
||||
if (stat == "mean"){
|
||||
data<-data %>% group_by({{var}}) %>% summarise("{{x}}":=mean({{x}}, na.rm=T), "{{y}}":=mean({{y}}, na.rm=T))
|
||||
}
|
||||
if (!is.null(color)){
|
||||
return(geom_label(data = data, aes({{x}},{{y}}, label={{var}}), color=color, ...))
|
||||
}else{
|
||||
return(geom_label(data = data, aes({{x}},{{y}}, label={{var}}),...))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
ggstats_add_xy<-function(table_stat, table, xcol=NULL, group, y="max", bracket.offset=0.05, bracket.inspace=0.05, exclude_group=NULL, dodge=0.75){
|
||||
## Adapted version to fit rstatix output
|
||||
value.var<-table_stat[[1,".y."]]
|
||||
if (is.null(xcol)){
|
||||
x<-colnames(table_stat)[1]
|
||||
}else{x<-xcol}
|
||||
|
||||
if(!is.null(group)){table[,group]<-as.factor(pull(table,group))}
|
||||
table[,x]<-as.factor(pull(table,x))
|
||||
if (is.null(exclude_group)){
|
||||
table_agg<-table %>% group_by(.data[[x]])
|
||||
}else{
|
||||
table_agg<-table %>% group_by(.data[[x]], !!!syms(exclude_group))
|
||||
}
|
||||
if (y == "max"){
|
||||
agg<-table_agg %>% summarise(max=max(.data[[value.var]], na.rm = T))
|
||||
if(!is.null(exclude_group)){agg<-table_agg %>% group_by(!!!syms(exclude_group)) %>% summarise(max=max(.data[[value.var]], na.rm=T))}
|
||||
}else if (y == "mean"){
|
||||
agg<-table %>% group_by(.data[[x]],.data[[group]]) %>% summarise(mean=mean(.data[[value.var]], na.rm=T)) %>% spread(group, mean)
|
||||
agg<- data.frame(x=agg[,1], "max"=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
|
||||
colnames(agg)[1]<-x
|
||||
}else if (y == "mean+sd"){
|
||||
agg<-table %>% group_by(.data[[x]],.data[[group]]) %>% summarise(mean=mean(.data[[value.var]], na.rm=T)+sd(.[[value.var]], na.rm=T)) %>% spread(group, mean)
|
||||
agg<- data.frame(x=agg[,1], "max"=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
|
||||
colnames(agg)[1]<-x
|
||||
}else if (y == "mean+sem"){
|
||||
agg<-table %>% group_by(.data[[x]],.data[[group]]) %>% summarise(mean=mean(.data[[value.var]], na.rm=T)+sem(.[[value.var]], na.rm=T)) %>% spread(group, mean)
|
||||
agg<- data.frame(x=agg[,1], "max"=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
|
||||
colnames(agg)[1]<-x
|
||||
}
|
||||
|
||||
group.list<-list()
|
||||
count<-1
|
||||
if(!is.null(group)){table_stat<-mutate(table_stat, {{x}}:=as.factor(.data[[x]]))}
|
||||
|
||||
for (i in 1:nrow(table_stat)){
|
||||
group.list[[count]]<-c(table_stat %>% slice(i) %>% pull(group1),table_stat%>% slice(i) %>% pull(group2))
|
||||
count<-count+1
|
||||
}
|
||||
if(!is.null(group)){
|
||||
x.index<-sapply(table_stat %>% pull(x), function(y) which(levels(table_stat %>% pull(x)) == y))
|
||||
t<-tibble("y.position"=merge(table_stat, agg ,sort=F)[,"max"]+diff(range(table[value.var], na.rm = T))*bracket.offset,
|
||||
"groups"=group.list,
|
||||
"x.temp"=x.index,
|
||||
"xmin"=(match(table_stat %>% pull(x), levels(table[,x]))+dodge*((match(table_stat$group1, levels(table[,group]))-0.5)/length(levels(table[,group]))-0.5)),
|
||||
"xmax"=match(table_stat %>% pull(x), unique(table[,x]))+dodge*((match(table_stat$group2, levels(table[,group]))-0.5)/length(levels(table[,group]))-0.5)
|
||||
) %>% rename("x"="x.temp")
|
||||
}else{
|
||||
t<-tibble("y.position"=merge(table_stat, agg ,sort=F)[,"max"]+diff(range(table[value.var], na.rm = T))*bracket.offset,
|
||||
"groups"=group.list,
|
||||
# "x.temp"=x.index,
|
||||
)# %>% rename("x"="x.temp")
|
||||
}
|
||||
if (!is.null(group)){
|
||||
if (!is.null(exclude_group)){
|
||||
for (j in unique(pull(table_stat, all_of(exclude_group)))){
|
||||
for (dia in unique(pull(table_stat,all_of(xcol)))){
|
||||
if (table_stat %>% dplyr::filter(p < 0.05) %>% dplyr::filter(.data[[x]] == dia & .data[[exclude_group]] == j) %>% nrow() > 0){
|
||||
t[table_stat[,x] == dia & table_stat[,exclude_group] == j,"y.position"]<-seq(t[table_stat[,x] == dia & table_stat[,exclude_group] == j,"y.position"][[1,1]],
|
||||
t[table_stat[,x] == dia & table_stat[,exclude_group] == j,"y.position"][[1,1]]+diff(range(table[,value.var], na.rm = T))*bracket.inspace*(nrow(table_stat[table_stat[,x] == dia & table_stat[,exclude_group] == j,])-1),
|
||||
by=diff(range(table[,value.var], na.rm = T))*bracket.inspace)
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
for (dia in unique(pull(table_stat,all_of(xcol)))){
|
||||
t[table_stat[,x] == dia,"y.position"]<-seq(t[table_stat[,x] == dia,"y.position"][[1,1]],
|
||||
t[table_stat[,x] == dia,"y.position"][[1,1]]+diff(range(table[,value.var], na.rm = T))*bracket.inspace*(nrow(table_stat[table_stat[,x] == dia,])-1),
|
||||
by=diff(range(table[,value.var], na.rm = T))*bracket.inspace)
|
||||
}
|
||||
}
|
||||
}else{
|
||||
for (dia in unique(pull(mutate(table_stat,dia=paste0(!!!syms(exclude_group))),dia))){
|
||||
t[apply(table_stat[,exclude_group], 1, paste, collapse="") == dia, "y.position"]<-seq(t[apply(table_stat[,exclude_group], 1, paste, collapse="") == dia,"y.position"][[1,1]],
|
||||
t[apply(table_stat[,exclude_group], 1, paste, collapse="") == dia,"y.position"][[1,1]]+
|
||||
diff(range(table[,value.var], na.rm = T))*
|
||||
bracket.inspace*(nrow(table_stat[apply(table_stat[,exclude_group], 1, paste, collapse="") == dia,])-1),
|
||||
by=diff(range(table[,value.var], na.rm = T))*bracket.inspace)
|
||||
}
|
||||
}
|
||||
return(cbind(table_stat,t) %>% as_tibble)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
md2csv<-function(text, rm_blank=T){
|
||||
text<-strsplit(text, "\n")[[1]]
|
||||
text<-gsub("^[ |]*|[|][ ]*$","",text)
|
||||
if(rm_blank){text<-gsub(" ","",text)}
|
||||
text<-gsub("[|]",",",text)
|
||||
return(cat(text,sep = "\n"))
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
perc<-function(x, per100=T){
|
||||
if (per100==T){
|
||||
return(x*100/sum(x, na.rm = T))
|
||||
}else{
|
||||
return(x/sum(x, na.rm = T))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
sem <- function(x, na.rm=F){
|
||||
sem<-sd(x, na.rm = na.rm)/sqrt(length(x))
|
||||
return(sem)
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
tab2md<-function(table){
|
||||
samples_csv<-table
|
||||
table<-apply(table, 1, paste, collapse="|")
|
||||
table<-gsub("^","|",table)
|
||||
table<-gsub("$","|",table)
|
||||
table<-c(paste0("|",paste(colnames(samples_csv), collapse="|"),"|"),
|
||||
paste0("|",paste(rep("---",ncol(samples_csv)), collapse="|"),"|"),
|
||||
table)
|
||||
cat(table, sep="\n")
|
||||
}
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
theme_heatmap<-function(x){
|
||||
list(geom_tile(),
|
||||
theme_heatmap<-function(x,line.color="#FFFFFF00"){
|
||||
list(geom_tile(color=line.color),
|
||||
scale_fill_gradientn(colors=col2(200)),
|
||||
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5),
|
||||
panel.background = element_blank(),
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# CitFuns
|
||||
|
||||
Este repositorio contiene un paquete de R que recopila diferentes funciones útiles para el laboratorio CIT del ICO, en Barcelona, aunque puede ser útil para cualquiera que haga análisis parecidos.
|
||||
|
||||
Para instalar el paquete se necesita tener instalado:
|
||||
|
||||
- [Rtools](https://cran.r-project.org/bin/windows/Rtools/rtools40.html)
|
||||
- [Git](https://git-scm.com/downloads)
|
||||
|
||||
Y las funciones dependen de los paquetes:
|
||||
|
||||
- tidyverse
|
||||
- Hmisc
|
||||
- gtools
|
||||
|
||||
Una vez cuplidas las dependencias, el paquete se instala así:
|
||||
|
||||
```r
|
||||
devtools::install_git("https://git.ratg.cat/marcelcosta/CitFuns.git", build_vignettes=T)
|
||||
```
|
||||
|
||||
Puedes consultar la documentación ejecutando:
|
||||
|
||||
```r
|
||||
browseVignettes("CitFuns")
|
||||
```
|
||||
@@ -0,0 +1,30 @@
|
||||
\name{ggcorrplot}
|
||||
\alias{ggcorrplot}
|
||||
\title{ggcorrplot}
|
||||
\usage{
|
||||
ggcorrplot(df, var, value, color="#FFFFFF00", stat="signif", tri="all", method="pearson")
|
||||
}
|
||||
\arguments{
|
||||
\item{df}{A data frame in "long" format.}
|
||||
\item{var}{The column that will be used to analyze correlation all against all.}
|
||||
\item{value}{The column that will be used as numeric data to analyze correlation.}
|
||||
\item{color}{The color of the lines of geom_tile (the border). By default, its transparent.}
|
||||
\item{stat}{The stats that will be on the tiles. They can be "signif", "pval", "none". By default, it presents "signif" (stars representing pvalue).}
|
||||
\item{tri}{It specifies which half of the correlation matrix is shown. Can be "all" (the default), "upper" or "lower".}
|
||||
\item{method}{Method for correlation. It will be passed to "cor" and "rcorr" functions, "pearson" by default.}
|
||||
}
|
||||
\description{
|
||||
Generates a correlation matrix using ggplot for a dataframe.
|
||||
}
|
||||
\examples{
|
||||
library(tidyverse)
|
||||
|
||||
df<-data.frame("pats"=paste0("PAT", 1:20), "CytA"=rnorm(20,5), "CytB"=rnorm(20,5),
|
||||
"CytC"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytD"=rnorm(20,5),
|
||||
"CytE"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytF"=rnorm(20,5),
|
||||
"CytG"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)))
|
||||
df<-gather(df, Cyt, Value,-pats)
|
||||
head(df)
|
||||
|
||||
ggcorrplot(df, Cyt, Value)
|
||||
}
|
||||
+9
-2
@@ -2,7 +2,7 @@
|
||||
\alias{ggheatmap}
|
||||
\title{ggheatmap}
|
||||
\usage{
|
||||
ggheatmap(df, x=NULL, y=NULL, value=NULL, grouping="mean")
|
||||
ggheatmap(df, x=NULL, y=NULL, value=NULL, grouping="mean", exclude_group=NULL, scale="none", color="#FFFFFF00")
|
||||
}
|
||||
\arguments{
|
||||
\item{df}{A data frame in "long" format.}
|
||||
@@ -10,6 +10,9 @@ ggheatmap(df, x=NULL, y=NULL, value=NULL, grouping="mean")
|
||||
\item{y}{The variable that will be used for Y axis in the heatmap. If not specified, the second column will be used.}
|
||||
\item{value}{The variable that will be used for colouring the heatmap. If not specified, the third column will be used.}
|
||||
\item{grouping}{The function that will be used to group if there is more than one observation for each X-Y combination. "mean" or "median" are posible, defaulting to "mean".}
|
||||
\item{exclude_group}{If you want to exclude columns from grouping, you can introduce a character vector here. Useful if you want to facet the plot.}
|
||||
\item{scale}{Escales (z-score) by "rows" or "cols" if diferent of "none" (default).}
|
||||
\item{color}{The color of the lines of geom_tile (the border). By default, its transparent.}
|
||||
}
|
||||
\description{
|
||||
Generates a heatmap using ggplot for a dataframe.
|
||||
@@ -17,7 +20,11 @@ Generates a heatmap using ggplot for a dataframe.
|
||||
\examples{
|
||||
library(tidyverse)
|
||||
|
||||
df<-data.frame("pats"=paste0("PAT", 1:20), "CytA"=rnorm(20,5), "CytB"=rnorm(20,5),"CytC"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytD"=rnorm(20,5),"CytE"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytF"=rnorm(20,5),"CytG"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5))) %>% gather(Cyt, Value,-pats)
|
||||
df<-data.frame("pats"=paste0("PAT", 1:20), "CytA"=rnorm(20,5), "CytB"=rnorm(20,5),
|
||||
"CytC"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytD"=rnorm(20,5),
|
||||
"CytE"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytF"=rnorm(20,5),
|
||||
"CytG"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)))
|
||||
df<-gather(df, Cyt, Value,-pats)
|
||||
head(df)
|
||||
|
||||
ggheatmap(df)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
\name{gglegend}
|
||||
\alias{gglegend}
|
||||
\title{gglegend}
|
||||
\usage{
|
||||
gglegend(data, x, y, var, stat="median", color="black", ...)
|
||||
}
|
||||
\arguments{
|
||||
\item{data}{A data frame from where to take x and y axis, and the grouping variable.}
|
||||
\item{x}{The variable that will be used for X axis in the heatmap.}
|
||||
\item{y}{The variable that will be used for Y axis in the heatmap.}
|
||||
\item{value}{The variable that will be used for grouping the heatmap. The labels will be taken from it}
|
||||
\item{stat}{The statistical central test that will be used to calculate the label coordinates. "mean" or "median" are posible, defaulting to "median".}
|
||||
\item{color}{The color of the labels, "black" by default. If NULL, nothig will be passed to the geom_label so it may be taken from aesthetics.}
|
||||
\item{...}{Other arguments that will be passed to the geom_label function.}
|
||||
}
|
||||
\description{
|
||||
Generates a geom_label object with labels in the center of each population or agrupation.
|
||||
}
|
||||
\examples{
|
||||
library(tidyverse)
|
||||
library(Rtsne)
|
||||
library(Rphenograph)
|
||||
library(igraph)
|
||||
|
||||
## We will generate an example using tSNE and Phenograph algorithms, but can be applied to any other situation.
|
||||
|
||||
iris_unique <- unique(iris) # Remove duplicates
|
||||
iris_matrix <- as.matrix(iris_unique[,1:4])
|
||||
|
||||
# Set a seed if you want reproducible results
|
||||
set.seed(42)
|
||||
tsne_out <- Rtsne(iris_matrix,pca=FALSE,perplexity=30,theta=0.0) # Run TSNE
|
||||
|
||||
rpheno<-Rphenograph(tsne_out$Y)
|
||||
df<-data.frame(as.data.frame(tsne_out$Y), "Clust"=factor(membership(rpheno[[2]])))
|
||||
head(df)
|
||||
|
||||
ggplot(df, aes(V1,V2, color=Clust))+
|
||||
geom_point()+
|
||||
gglegend(df, V1, V2, Clust)+
|
||||
guides(color="none")
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
\name{ggstats_add_xy}
|
||||
\alias{ggstats_add_xy}
|
||||
\title{ggstats_add_xy}
|
||||
\usage{
|
||||
ggstats_add_xy(table_stat, table, group, xcol=NULL, y="max", bracket.offset=0.05, bracket.inspace=0.05, exclude_group=NULL, dodge=0.75)
|
||||
}
|
||||
\arguments{
|
||||
\item{table_stat}{A table generated by a rstatix funcion such as t_test() or wilcox_test().}
|
||||
\item{table}{The original table that was introduced to ggplot.}
|
||||
\item{xcol}{The X-axis variable. If null (the default), the function will pick the first column. This is intended when exclude_group is null.}
|
||||
\item{group}{The grouping variable. If null, the function will emulate the add_y_position function instead of add_xy_position.}
|
||||
\item{y}{The algorithm used to calculate the y height. Is useful to adapt to plots that show all events, bars with the mean, etc... Options are "max" (default), "mean", "mean+sd", "mean+sem".}
|
||||
\item{bracket.offset}{Increasing this parameter incresases the y position of the brackets.}
|
||||
\item{bracket.inspace}{Increasing this parameter increases the space between brackets.}
|
||||
\item{exclude_group}{Variable(s) that will not be grouped. This aims to preserve the variable in order to facet.}
|
||||
\item{dodge}{How much dodge to perform (different ggplot geoms have different dodge values).}
|
||||
}
|
||||
\description{
|
||||
Generates a table with the coordinates that is required by the ggpubr function "stat_pvalue_manual".
|
||||
}
|
||||
\examples{
|
||||
library(tidyverse)
|
||||
library(ggpubr)
|
||||
library(rstatix)
|
||||
|
||||
|
||||
df<-data.frame("pats"=paste0("PAT", 1:20), "Group"=rep(c("A","B"),each=10),"CytA"=rnorm(20,5), "CytB"=rnorm(20,5),"CytC"=c(rnorm(5,10),rnorm(5,10),rnorm(5,5),rnorm(5,5)),"CytD"=rnorm(20,5),"CytE"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytF"=rnorm(20,5),"CytG"=c(rnorm(5,5),rnorm(5,5),rnorm(5,7),rnorm(5,7)))
|
||||
|
||||
mdf<-gather(df, "Cytokine","Value",-c(pats,Group))
|
||||
|
||||
g<-ggplot(mdf, aes(Cytokine, Value, color=Group))+
|
||||
geom_boxplot()+
|
||||
geom_point(position=position_dodge(width = 0.75))
|
||||
|
||||
stat.test<-mdf \%>\% group_by(Cytokine) \%>\% t_test(Value~Group)
|
||||
g+stat_pvalue_manual(ggstats_add_xy(stat.test, mdf, "Cytokine","Group"), label = "p", tip.length = 0.02, hide.ns = T)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
\name{md2csv}
|
||||
\alias{md2csv}
|
||||
\title{md2csv}
|
||||
\usage{
|
||||
md2csv(text, rm_blank =T)
|
||||
}
|
||||
\arguments{
|
||||
\item{text}{An md text that you want to convert to csv. Note that you must eliminate the line that separates title from the rest.}
|
||||
\item{text}{Bolean to specify if you want to remove spaces or not.}
|
||||
}
|
||||
\description{
|
||||
This function converts an Markdown (md) table into csv format.
|
||||
}
|
||||
\examples{
|
||||
text<-"|Letter|Number|
|
||||
|A| 1|
|
||||
|B| 2|
|
||||
|C| 3|
|
||||
|D| 4|
|
||||
|E| 5|
|
||||
|F| 6|
|
||||
|G| 7|
|
||||
|H| 8|
|
||||
|I| 9|
|
||||
|J|10|"
|
||||
md2csv(text)
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
\name{perc}
|
||||
\alias{perc}
|
||||
\title{perc}
|
||||
\usage{
|
||||
perc(x, per100=T)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{A numeric vector or an R object but not a factor coercible to numeric by as.double(x).}
|
||||
\item{perc}{A logical value indicating whether the result must sum 100 (TRUE) or 1 (FALSE).}
|
||||
}
|
||||
\description{
|
||||
This function the percentages of a numeric vector.
|
||||
}
|
||||
\examples{
|
||||
v<-c(2,5,10,3)
|
||||
perc(v)
|
||||
|
||||
# It can be used with dplyr tables
|
||||
library(tidyverse)
|
||||
df<-data.frame("X"=c("A","A","B","B"), "Y"=v)
|
||||
df \%>\% mutate(Y=perc(Y))
|
||||
|
||||
# Or it can be under 1
|
||||
df \%>\% mutate(Y=perc(Y, per100=F))
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
\name{sem}
|
||||
\alias{sem}
|
||||
\title{sem}
|
||||
\usage{
|
||||
sem(x, na.rm=T)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{A numeric vector or an R object but not a factor coercible to numeric by as.double(x).}
|
||||
\item{na.rm}{A logical value indicating whether NA values should be stripped before the computation proceeds.}
|
||||
}
|
||||
\description{
|
||||
This function computes the Standard Error of the Mean of the values in x. If na.rm is TRUE then missing values are removed before computation proceeds.
|
||||
}
|
||||
\examples{
|
||||
v<-rnorm(10)
|
||||
sem(v)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
\name{tab2md}
|
||||
\alias{tab2md}
|
||||
\title{tab2md}
|
||||
\usage{
|
||||
tab2md(text)
|
||||
}
|
||||
\arguments{
|
||||
\item{text}{A data.frame that you want to convert to Markdown.}
|
||||
}
|
||||
\description{
|
||||
This function converts a data.frame into Markdown (md) format.
|
||||
}
|
||||
\examples{
|
||||
table<-data.frame("Letter"=LETTERS[1:10], "Number"=1:10)
|
||||
tab2md(table)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
---
|
||||
title: "CitFuns"
|
||||
output: rmarkdown::html_vignette
|
||||
description: |
|
||||
How to use CitFuns functions.
|
||||
vignette: >
|
||||
%\VignetteIndexEntry{CitFuns Package}
|
||||
%\VignetteEngine{knitr::rmarkdown}
|
||||
%\VignetteEncoding{UTF-8}
|
||||
---
|
||||
|
||||
|
||||
```{r setup, include=FALSE}
|
||||
knitr::opts_chunk$set(echo = TRUE, message = F, warning = F, error = F)
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
In order to install CitFuns package from Git reppository, you must install `devtools` package:
|
||||
|
||||
```{r, eval=F}
|
||||
install.packages('devtools')
|
||||
```
|
||||
|
||||
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).
|
||||
|
||||
You will also need [Git](https://git-scm.com/downloads) installed in your computer.
|
||||
|
||||
Now we are ready to install CitFuns package:
|
||||
```{r, eval=F}
|
||||
devtools::install_git("https://git.ratg.cat/marcelcosta/CitFuns.git", build_vignettes = T)
|
||||
```
|
||||
|
||||
## Update
|
||||
|
||||
Any time you want to update the package, you must *reinstall* it:
|
||||
|
||||
```{r, eval=F}
|
||||
detach("package:CitFuns", unload = TRUE) # Only required if you have loaded the package in this session.
|
||||
devtools::install_git("https://git.ratg.cat/marcelcosta/CitFuns.git")
|
||||
```
|
||||
|
||||
## ggheatmap
|
||||
|
||||
**ggheatmap** generates ggplot heatmaps easily.
|
||||
|
||||
We start by loading required packages, including *CitFuns*
|
||||
|
||||
```{r}
|
||||
library(tidyverse)
|
||||
library(CitFuns)
|
||||
```
|
||||
|
||||
Now we will create an example dataframe:
|
||||
|
||||
```{r}
|
||||
df<-data.frame("pats"=paste0("PAT", 1:20), "CytA"=rnorm(20,5), "CytB"=rnorm(20,5),
|
||||
"CytC"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytD"=rnorm(20,5),
|
||||
"CytE"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)),"CytF"=rnorm(20,5),
|
||||
"CytG"=c(rnorm(5,10),rnorm(5,5),rnorm(5,10),rnorm(5,5)))
|
||||
|
||||
df<-gather(df, Cyt, Value,-pats)
|
||||
head(df)
|
||||
```
|
||||
|
||||
Usually, this package works with the dataframes in **Long** format, as it is intended in ggplot workflow.
|
||||
|
||||
And now we will generate the heatmap.
|
||||
|
||||
```{r, fig.width=8}
|
||||
ggheatmap(df)
|
||||
```
|
||||
|
||||
As we can observe, X and Y axis are sorted by cluster detection (using *hclust*).
|
||||
|
||||
*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.
|
||||
|
||||
```{r, fig.width=8}
|
||||
clinics<-data.frame("pats"=df$pats %>% unique, "Met"=rep(c("0","1"), 10))
|
||||
df<-merge(df, clinics)
|
||||
|
||||
ggheatmap(df, x="Cyt",y="Met", grouping = "median")
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
```{r, fig.width=8}
|
||||
ggheatmap(df, exclude_group = "Met")+facet_grid(.~Met, scales = "free")
|
||||
```
|
||||
|
||||
You can specify a color instead of being transparent to the tiles:
|
||||
```{r, fig.width=8}
|
||||
ggheatmap(df, color="black")
|
||||
```
|
||||
|
||||
Finally, you can scale the heatmap either by rows or by columns:
|
||||
|
||||
```{r, fig.width=8}
|
||||
ggheatmap(df, scale="rows")
|
||||
```
|
||||
|
||||
It is worth noticing that ggheatmap outputs a ggplot object, so you can further modify it as you are used to:
|
||||
|
||||
```{r, fig.width=8, fig.height=5}
|
||||
ggheatmap(df)+
|
||||
scale_fill_gradient(low = "black", high = "yellow")+
|
||||
scale_y_discrete(position = "right")+
|
||||
theme(legend.position = "bottom")
|
||||
```
|
||||
|
||||
## ggcorrplot
|
||||
|
||||
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.
|
||||
|
||||
```{r, fig.width=5, fig.height=4}
|
||||
ggcorrplot(df, var = "Cyt", value = "Value")
|
||||
```
|
||||
|
||||
You can specify a color for the tile lines, transparent by default:
|
||||
|
||||
```{r, fig.width=5, fig.height=4}
|
||||
ggcorrplot(df, var = "Cyt", value = "Value", color = "white")
|
||||
```
|
||||
|
||||
By default, ggcorrplot converts the p-value into *star* significance equivalence. You can show the pvalue or nothing ("none").
|
||||
|
||||
```{r, fig.width=5, fig.height=4}
|
||||
ggcorrplot(df, var = "Cyt", value = "Value", color = "white", stat="pval")
|
||||
```
|
||||
|
||||
ggcorrplot uses "pearson" by default to obtain a correlation coeficient, although "spearman" is also available.
|
||||
|
||||
```{r, fig.width=5, fig.height=4}
|
||||
ggcorrplot(df, var = "Cyt", value = "Value", color = "white", stat="pval", method="spearman")
|
||||
```
|
||||
|
||||
|
||||
Finally, you can show only the upper part or the lower part of the specular matrix.
|
||||
|
||||
```{r, fig.width=5, fig.height=4}
|
||||
ggcorrplot(df, var = "Cyt", value = "Value", color = "black", tri="lower")
|
||||
```
|
||||
|
||||
## gglegend
|
||||
|
||||
gglegend generates a geom_label that calculates the median or mean coordinate from a group in a dotplot with two numerical axis. This is specially designed for results from tSNE+Phenograph cytometry analysis or scRNAseq analysis.
|
||||
|
||||
Lets use some coordinates generated from tSNE and Phenograph alghorithms.
|
||||
```{r, collapse=TRUE}
|
||||
v1<-c("12.2003375696557","9.61179669498037","9.71523107560819","9.36085913975228","12.4250267807441","14.3028296270378","10.1352873281603","11.5029598315655","8.56811495574298","10.0823452299921","13.6266459325397","10.9933175551642","9.33699055226273","8.4753653954153","14.8718666962033","15.2360998395043","14.2924029287808","12.3444533761381","14.2993507234786","13.4864775273824","12.7460734690735","13.161500730014","10.1359520632972","12.2185983825899","11.2616934436752","9.90490138562409","11.9609343515576","12.5884534548523","12.0364418615964","10.1870261767374","10.0422315951637","12.7783195658305","14.5992551781486","14.9493239728631","10.158835739269","10.6054386317344","13.2848626188882","11.936894948252","8.7128504500476","11.7737895359261","11.8983735877432","8.02689567143797","9.12243498461827","12.6527770922061","13.9105401986929","9.35741259585257","13.5911333739262","9.58527361989105","13.4456024538875","11.0895916895462","-5.85077686923467","-4.52715357511742","-6.29563605082642","0.575879987115163","-5.00038311800316","-1.78476165367942","-4.78291504182624","2.14145872648519","-4.95470848078674","0.527964928988151","2.0407874996449","-2.01707703338718","0.295902773532714","-4.00219087667774","0.646856668064106","-4.88234548321471","-2.03349635088723","-0.49193567263671","-2.79335406976441","0.448763877743354","-4.75643812997927","-1.42563453004395","-6.23450075793563","-3.62615912431353","-3.76809728669077","-4.60208220139584","-5.85501795453493","-7.05239199090951","-3.20043054503014","1.15876766287925","0.895292755360344","1.14444064679356","-0.176884624041849","-6.35832840326582","-1.65398473342688","-3.79355073906942","-5.5958811065813","-2.73187337265253","-0.874422976991474","0.261500351579398","-0.561805573681923","-3.75892543868909","-0.236866399218848","2.09008746058036","-0.548227337920367","-1.21516952408529","-1.17389146089581","-2.97942173316255","2.22364389707379","-0.740849099358617","-11.53883548612","-6.10528935020874","-12.060454035126","-9.30052482902171","-10.8000103031182","-13.5180286021874","-0.350552508950501","-12.8614336490877","-10.5151593701859","-12.7997253357382","-8.9616405672664","-8.4882832838348","-10.5542279802031","-6.07143684739709","-6.38549255444235","-10.0449160880572","-9.44413278886994","-13.777705761209","-13.7964826297576","-6.8363880475111","-11.5388213442119","-5.6282354747176","-13.6138636099046","-6.381104642862","-11.1812519418261","-12.3831679874129","-5.77325698424297","-5.48616485259332","-9.96244295723046","-12.114107326341","-12.7616465926232","-13.7599167738886","-10.1159812826476","-6.62864900674468","-8.06745259542743","-13.1987304489436","-10.8870376481335","-9.36038405695135","-5.13930391264302","-10.5111656232542","-10.9829759447952","-9.82553470541781","-11.7458951813431","-11.4450763813922","-9.87343759006578","-6.95726085424341","-9.03829069857169","-10.4246847556504","-5.70838830660222")
|
||||
v2<-c("21.0246799882543","21.1604668637415","22.3065974726199","22.167058907011","21.485793337356","21.034495141115","22.8527942337251","21.4484506937578","22.2141672875484","21.345153106709","20.4880451876175","22.2111273969088","21.4727323576141","22.8241608452394","20.0044199930559","20.4988095357755","20.5599765528411","21.1969434679012","20.0020746075624","21.4337624510195","19.9736219509137","21.5709896392616","23.6577471746758","22.5535814744334","22.8384868736052","20.8650810158446","22.1613883712501","20.7983446457222","20.5655252200496","22.2090780986936","21.7806687207636","20.1194553082707","21.3069459537366","20.662407531373","21.4251709330106","20.765690762975","19.8859038873819","21.7056631952668","22.5041194163155","20.9642232055426","21.3422306162038","21.6367978620734","22.9262096626072","22.4646597147181","22.1141613861558","21.5054873462004","21.4889032804239","22.5008054544748","20.76786026091","21.2016450580895","-8.72858183844766","-9.95487383065406","-9.0344478298343","-10.2479477494554","-9.75424269139439","-11.3104503130975","-10.6530974583924","-10.84576176497","-9.3121096499565","-11.3954821057958","-10.459830426721","-10.7685510257121","-9.6028748910031","-10.8250931981046","-11.4576048366608","-8.99623858309278","-11.6991335601257","-10.3000992136515","-8.80499949660716","-10.5287181221244","-12.1008107052572","-10.0265692008998","-10.9708704570088","-10.5302978352576","-9.54786020396403","-9.23397400708739","-9.24386152038901","-9.65261439968974","-10.9281368746817","-10.9473794707607","-10.4850238433817","-10.4882084917208","-10.4990514736718","-12.0128979043322","-11.941610711928","-11.6458333185284","-9.24844012046275","-8.88646414055994","-11.3227097840251","-10.8268151538455","-11.5279408155446","-10.7575990470602","-10.360283532249","-10.7913818861931","-11.091926796243","-11.0159937813687","-10.982258258046","-10.0920126917865","-10.9583452116076","-10.8108899751332","-9.56368334300102","-12.8367178840931","-11.3709591119047","-11.652506805406","-11.1169982547012","-11.6968611726564","-8.86077529188209","-11.9662163203006","-12.2158702454966","-10.5422338216268","-10.1439305299441","-11.3349056863203","-10.8133385736638","-13.1632816376455","-13.5059352283161","-9.88618491633494","-11.2477309742765","-10.9615658268355","-12.0131549369708","-12.187081729914","-10.6769895449992","-13.1713800408861","-11.9147981603324","-11.3127268327162","-10.6744434631015","-11.6870047899695","-11.4368856559989","-11.7991750334205","-11.4332997824959","-12.0583096122799","-11.9469443936353","-10.9598799506617","-11.3275484766061","-11.0525813106846","-12.3015921365232","-11.4579022030789","-9.5065760211426","-11.203396001218","-11.9253283065003","-10.5294239353879","-10.3400935752971","-9.87954083672559","-10.6401156456368","-10.0740874914124","-10.1578866019443","-11.5501223255682","-10.5308167593782","-9.35047777292819","-12.4071042980126")
|
||||
clust<-c("1","2","2","2","1","1","2","1","2","2","1","2","2","2","1","1","1","1","1","1","1","1","2","1","2","2","1","1","1","2","2","1","1","1","2","2","1","1","2","1","1","2","2","1","1","2","1","2","1","2","3","3","3","4","3","4","3","4","3","4","4","4","4","3","4","3","4","4","4","4","3","4","3","3","3","3","3","3","3","4","4","4","4","3","4","3","3","4","4","4","4","3","4","4","4","4","4","4","4","4","5","3","5","5","5","5","4","5","5","5","5","5","5","3","3","5","5","5","5","3","5","3","5","3","5","5","3","3","5","5","5","5","5","3","3","5","5","5","3","5","5","5","5","5","5","3","5","5","3")
|
||||
```
|
||||
|
||||
|
||||
```{r}
|
||||
df<-data.frame("V1"=as.numeric(v1),"V2"=as.numeric(v2),"Clust"=clust)
|
||||
head(df)
|
||||
```
|
||||
|
||||
Usually, you would do:
|
||||
```{r}
|
||||
ggplot(df, aes(V1,V2, color=Clust))+
|
||||
geom_point()
|
||||
```
|
||||
|
||||
But the colors might be confusing when having more than 8 categories. It's easier to do:
|
||||
```{r}
|
||||
ggplot(df, aes(V1,V2, color=Clust))+
|
||||
geom_point()+
|
||||
gglegend(df, V1, V2, Clust)+
|
||||
guides(color="none")
|
||||
```
|
||||
|
||||
## SEM
|
||||
|
||||
sem is just a small implementation to calculate the standard error of the mean. Useful for ploting errorbars with it.
|
||||
|
||||
```{r}
|
||||
v<-rnorm(10)
|
||||
sem(v)
|
||||
```
|
||||
|
||||
## perc
|
||||
|
||||
perc transforms a numeric vector to percentage. Useful when using dplyr summarise function.
|
||||
|
||||
```{r}
|
||||
v<-c(2,5,10,3)
|
||||
perc(v)
|
||||
|
||||
# It can be used with dplyr tables
|
||||
library(tidyverse)
|
||||
df<-data.frame("X"=c("A","A","B","B"), "Y"=v)
|
||||
df %>% group_by(X) %>% summarise(Y=perc(Y))
|
||||
|
||||
# Or it can be under 1
|
||||
df %>% group_by(X) %>% summarise(Y=perc(Y, per100=F))
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user