Browse Source

Añadida función ggstats_add_xy

main
Costa 2 years ago
parent
commit
9fe57a3f32
2 changed files with 75 additions and 0 deletions
  1. +42
    -0
      R/ggstats_add_xy.R
  2. +33
    -0
      man/ggstats_add_xy.Rd

+ 42
- 0
R/ggstats_add_xy.R

@ -0,0 +1,42 @@
ggstats_add_xy<-function(table_stat, table, group, y="max", bracket.offset=0.05, bracket.inspace=0.05){
## Adapted version to fit rstatix output
value.var<-table_stat[[1,".y."]]
x<-colnames(table_stat)[1]
table[,group]<-as.factor(table[,group])
table[,x]<-as.factor(table[,x])
if (y == "max"){
agg<-table %>% group_by(.data[[x]]) %>% summarise(max=max(.data[[value.var]]))
}else if (y == "mean"){
agg<-table %>% group_by(.data[[x]],.data[[group]]) %>% summarise(mean=mean(.data[[value.var]])) %>% 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]])+sd(.[[value.var]])) %>% 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]])+sem(.[[value.var]])) %>% 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
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")
for (dia in unique(pull(table_stat,1))){
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)
}
return(cbind(table_stat,t) %>% as_tibble)
}

+ 33
- 0
man/ggstats_add_xy.Rd

@ -0,0 +1,33 @@
\name{ggstats_add_xy}
\alias{ggstats_add_xy}
\title{ggstats_add_xy}
\usage{
ggstats_add_xy(table_stat, table, group, y="max", bracket.offset=0.05, bracket.inspace=0.05)
}
\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{group}{The grouping variable.}
\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}{Incresaing this parameter increases the space between brackets.}
}
\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, "Group"), label = "p", tip.length = 0.02, hide.ns = T)

Loading…
Cancel
Save