Compare commits

...

19 Commits

Author SHA1 Message Date
marcelcosta c88359c801 Specify package for "filter" function 2025-04-15 11:38:56 +02:00
marcelcosta a9bff2e058 Update 'R/ggstats_add_xy.R'
change !!!sym_link for .data[[exclude_group]].
2025-03-27 15:06:00 +01:00
marcelcosta 217bca2480 Correct for more than 1 facet. 2025-03-06 13:46:43 +01:00
marcelcosta ab96f5ec6a .data pronoun only allows one facet variable. I have changed into !!!syms(). 2025-03-06 13:12:00 +01:00
marcelcosta d42e4ff6f3 Change error stat.test for table_stat 2025-03-05 14:54:15 +01:00
marcelcosta 9ef27ea7de Add dodge option to ggstats_add_xy 2024-10-25 15:06:19 +02:00
marcelcosta a4483c84e6 Adding md2csv and tab2md functions. 2024-10-11 10:42:29 +02:00
marcelcosta 0cb487caa4 Correcting bugs. 2023-02-07 11:49:16 +01:00
marcelcosta c1bd92a7c9 Corregir bug, en cada panel salía misma y. 2023-01-24 15:06:15 +01:00
marcelcosta cde38f8aa6 Corregir nombre de función. 2023-01-24 14:31:13 +01:00
marcelcosta 296d98706a Corregir ejemplo. 2023-01-24 14:19:24 +01:00
marcelcosta ed125951a8 Cambiar orden parámetros. 2023-01-24 14:18:59 +01:00
marcelcosta 389b97d3b4 Adaptada la función para permitir comparaciones entre elementos de la x, no sólo agrupados. 2023-01-24 14:17:22 +01:00
marcelcosta de89c5258d Corregir error group->xcol 2023-01-24 12:58:23 +01:00
marcelcosta f44ec4b7fe Update 'DESCRIPTION' 2022-09-01 17:10:08 +02:00
marcelcosta d4521b874c Update 'DESCRIPTION' 2022-09-01 17:09:00 +02:00
Costa 9ab832c900 Solución de algún bug con el ordenamiento de elementos == 1. 2022-03-24 12:32:48 +01:00
Costa e57141aa22 Hacer que cuando haya un eje que sólo tenga un elemento ordene el otro. 2022-03-24 12:17:32 +01:00
Costa bd98a08c31 Hacer que no ordene si una de las dimensiones no tiene más de un elemento para que no de error en el hclust. 2022-03-24 12:08:43 +01:00
8 changed files with 144 additions and 34 deletions
+1
View File
@@ -7,5 +7,6 @@ Maintainer: Marcel Costa <marcelcosta@mcosta.cat>
Description: Funciones internas para el laboratorio CIT Description: Funciones internas para el laboratorio CIT
License: GPL License: GPL
Encoding: UTF-8 Encoding: UTF-8
Suggests: rmarkdown
VignetteBuilder: knitr VignetteBuilder: knitr
LazyData: true LazyData: true
+27 -1
View File
@@ -16,7 +16,33 @@ ggheatmap<-function(df, x=NULL, y=NULL, value=NULL, grouping="mean", exclude_gro
if (grouping == "median"){ if (grouping == "median"){
df<-df %>% summarise(Value=median(Value)) %>% ungroup df<-df %>% summarise(Value=median(Value)) %>% ungroup
} }
order<-clustsort(df %>% spread(VarY,Value) %>% select(!all_of(exclude_group)) %>% 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 != "none"){
if (scale == "rows"){ if (scale == "rows"){
+49 -29
View File
@@ -1,62 +1,82 @@
ggstats_add_xy<-function(table_stat, table, group, xcol=NULL, 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}
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.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
} }
group.list<-list() group.list<-list()
count<-1 count<-1
table_stat<-mutate(table_stat, {{x}}:=as.factor(.data[[x]])) 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}
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]))+0.75*((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)
) %>% rename("x"="x.temp")
if (!is.null(exclude_group)){ for (i in 1:nrow(table_stat)){
for (j in unique(pull(table_stat, all_of(exclude_group)))){ group.list[[count]]<-c(table_stat %>% slice(i) %>% pull(group1),table_stat%>% slice(i) %>% pull(group2))
for (dia in unique(pull(table_stat,all_of(xcol)))){ count<-count+1
if (stat.test %>% filter(p < 0.05) %>% 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]], if(!is.null(group)){
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), x.index<-sapply(table_stat %>% pull(x), function(y) which(levels(table_stat %>% pull(x)) == y))
by=diff(range(table[,value.var], na.rm = T))*bracket.inspace) 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{ }else{
for (dia in unique(pull(table_stat,all_of(xcol)))){ for (dia in unique(pull(mutate(table_stat,dia=paste0(!!!syms(exclude_group))),dia))){
t[table_stat[,x] == dia,"y.position"]<-seq(t[table_stat[,x] == dia,"y.position"][[1,1]], 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[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), t[apply(table_stat[,exclude_group], 1, paste, collapse="") == dia,"y.position"][[1,1]]+
by=diff(range(table[,value.var], na.rm = T))*bracket.inspace) 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")
}
+5 -4
View File
@@ -2,17 +2,18 @@
\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().}
\item{table}{The original table that was introduced to ggplot.} \item{table}{The original table that was introduced to ggplot.}
\item{group}{The grouping variable.} \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 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{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.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".
@@ -32,5 +33,5 @@ g<-ggplot(mdf, aes(Cytokine, Value, color=Group))+
geom_point(position=position_dodge(width = 0.75)) geom_point(position=position_dodge(width = 0.75))
stat.test<-mdf \%>\% group_by(Cytokine) \%>\% t_test(Value~Group) stat.test<-mdf \%>\% group_by(Cytokine) \%>\% t_test(Value~Group)
g+stat_pvalue_manual(ggstats_add_xy(stat.test, mdf, "Group"), label = "p", tip.length = 0.02, hide.ns = T) g+stat_pvalue_manual(ggstats_add_xy(stat.test, mdf, "Cytokine","Group"), label = "p", tip.length = 0.02, hide.ns = T)
} }
+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)
}