diff --git a/R/ggstats_add_xy.R b/R/ggstats_add_xy.R index 9946662..49b2825 100644 --- a/R/ggstats_add_xy.R +++ b/R/ggstats_add_xy.R @@ -1,4 +1,4 @@ -ggstats_add_xy<-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 value.var<-table_stat[[1,".y."]] if (is.null(xcol)){ @@ -14,7 +14,7 @@ ggstats_add_xy<-function(table_stat, table, xcol=NULL, group, y="max", bracket.o } if (y == "max"){ 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]], na.rm=T))} + if(!is.null(exclude_group)){agg<-table_agg %>% group_by(.data[[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)) @@ -42,8 +42,8 @@ ggstats_add_xy<-function(table_stat, table, xcol=NULL, group, y="max", bracket.o 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) + "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, diff --git a/man/ggstats_add_xy.Rd b/man/ggstats_add_xy.Rd index 2676510..e0e41d2 100644 --- a/man/ggstats_add_xy.Rd +++ b/man/ggstats_add_xy.Rd @@ -2,7 +2,7 @@ \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) +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().} @@ -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.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".