Compare commits

..

10 Commits

6 changed files with 84 additions and 13 deletions
+20 -12
View File
@@ -1,30 +1,30 @@
ggstats_add_xy_test<-function(table_stat, table, xcol=NULL, group, y="max", bracket.offset=0.05, bracket.inspace=0.05, exclude_group=NULL){ 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 ## Adapted version to fit rstatix output
value.var<-table_stat[[1,".y."]] value.var<-table_stat[[1,".y."]]
if (is.null(xcol)){ if (is.null(xcol)){
x<-colnames(table_stat)[1] x<-colnames(table_stat)[1]
}else{x<-xcol} }else{x<-xcol}
if(!is.null(group)){table[,group]<-as.factor(table[,group])} if(!is.null(group)){table[,group]<-as.factor(pull(table,group))}
table[,x]<-as.factor(table[,x]) table[,x]<-as.factor(pull(table,x))
if (is.null(exclude_group)){ if (is.null(exclude_group)){
table_agg<-table %>% group_by(.data[[x]]) table_agg<-table %>% group_by(.data[[x]])
}else{ }else{
table_agg<-table %>% group_by(.data[[x]], .data[[exclude_group]]) table_agg<-table %>% group_by(.data[[x]], !!!syms(exclude_group))
} }
if (y == "max"){ if (y == "max"){
agg<-table_agg %>% summarise(max=max(.data[[value.var]])) agg<-table_agg %>% summarise(max=max(.data[[value.var]], na.rm = T))
if(!is.na(exclude_group)){agg<-table_agg %>% group_by(.data[[exclude_group]]) %>% summarise(max=max(.data[[value.var]]))} 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"){ }else if (y == "mean"){
agg<-table %>% group_by(.data[[x]],.data[[group]]) %>% summarise(mean=mean(.data[[value.var]])) %>% spread(group, 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)) agg<- data.frame(x=agg[,1], "max"=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
colnames(agg)[1]<-x colnames(agg)[1]<-x
}else if (y == "mean+sd"){ }else if (y == "mean+sd"){
agg<-table %>% group_by(.data[[x]],.data[[group]]) %>% summarise(mean=mean(.data[[value.var]])+sd(.[[value.var]])) %>% spread(group, mean) 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)) agg<- data.frame(x=agg[,1], "max"=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
colnames(agg)[1]<-x colnames(agg)[1]<-x
}else if (y == "mean+sem"){ }else if (y == "mean+sem"){
agg<-table %>% group_by(.data[[x]],.data[[group]]) %>% summarise(mean=mean(.data[[value.var]])+sem(.[[value.var]])) %>% spread(group, mean) 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)) agg<- data.frame(x=agg[,1], "max"=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
colnames(agg)[1]<-x colnames(agg)[1]<-x
} }
@@ -42,8 +42,8 @@ ggstats_add_xy_test<-function(table_stat, table, xcol=NULL, group, y="max", brac
t<-tibble("y.position"=merge(table_stat, agg ,sort=F)[,"max"]+diff(range(table[value.var], na.rm = T))*bracket.offset, t<-tibble("y.position"=merge(table_stat, agg ,sort=F)[,"max"]+diff(range(table[value.var], na.rm = T))*bracket.offset,
"groups"=group.list, "groups"=group.list,
"x.temp"=x.index, "x.temp"=x.index,
"xmin"=(match(table_stat %>% pull(x), levels(table[,x]))+0.75*((match(table_stat$group1, levels(table[,group]))-0.5)/length(levels(table[,group]))-0.5)), "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]))+0.75*((match(table_stat$group2, 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") ) %>% rename("x"="x.temp")
}else{ }else{
t<-tibble("y.position"=merge(table_stat, agg ,sort=F)[,"max"]+diff(range(table[value.var], na.rm = T))*bracket.offset, t<-tibble("y.position"=merge(table_stat, agg ,sort=F)[,"max"]+diff(range(table[value.var], na.rm = T))*bracket.offset,
@@ -55,7 +55,7 @@ ggstats_add_xy_test<-function(table_stat, table, xcol=NULL, group, y="max", brac
if (!is.null(exclude_group)){ if (!is.null(exclude_group)){
for (j in unique(pull(table_stat, all_of(exclude_group)))){ for (j in unique(pull(table_stat, all_of(exclude_group)))){
for (dia in unique(pull(table_stat,all_of(xcol)))){ for (dia in unique(pull(table_stat,all_of(xcol)))){
if (stat.test %>% filter(p < 0.05) %>% filter(.data[[x]] == dia & .data[[exclude_group]] == j) %>% nrow() > 0){ 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"]<-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), 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) by=diff(range(table[,value.var], na.rm = T))*bracket.inspace)
@@ -69,6 +69,14 @@ ggstats_add_xy_test<-function(table_stat, table, xcol=NULL, group, y="max", brac
by=diff(range(table[,value.var], na.rm = T))*bracket.inspace) 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) return(cbind(table_stat,t) %>% as_tibble)
} }
+7
View File
@@ -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"))
}
+10
View File
@@ -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 -1
View File
@@ -2,7 +2,7 @@
\alias{ggstats_add_xy} \alias{ggstats_add_xy}
\title{ggstats_add_xy} \title{ggstats_add_xy}
\usage{ \usage{
ggstats_add_xy(table_stat, table, group, xcol=NULL, y="max", bracket.offset=0.05, bracket.inspace=0.05, exclude_group=NULL) 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{ \arguments{
\item{table_stat}{A table generated by a rstatix funcion such as t_test() or wilcox_test().} \item{table_stat}{A table generated by a rstatix funcion such as t_test() or wilcox_test().}
@@ -13,6 +13,7 @@ ggstats_add_xy(table_stat, table, group, xcol=NULL, y="max", bracket.offset=0.05
\item{bracket.offset}{Increasing this parameter incresases the y position of the brackets.} \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{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{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{ \description{
Generates a table with the coordinates that is required by the ggpubr function "stat_pvalue_manual". Generates a table with the coordinates that is required by the ggpubr function "stat_pvalue_manual".
+28
View File
@@ -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)
}
+17
View File
@@ -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)
}