Browse Source

Add dodge option to ggstats_add_xy

main
marcelcosta 1 month ago
parent
commit
9ef27ea7de
2 changed files with 6 additions and 5 deletions
  1. +4
    -4
      R/ggstats_add_xy.R
  2. +2
    -1
      man/ggstats_add_xy.Rd

+ 4
- 4
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 ## 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)){
@ -14,7 +14,7 @@ ggstats_add_xy<-function(table_stat, table, xcol=NULL, group, y="max", bracket.o
} }
if (y == "max"){ if (y == "max"){
agg<-table_agg %>% summarise(max=max(.data[[value.var]], na.rm = T)) 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"){ }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<-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))
@ -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, 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)),
"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") ) %>% 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,

+ 2
- 1
man/ggstats_add_xy.Rd

@ -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".

Loading…
Cancel
Save