From 8fe1c77f7434c6a3d183aaa76f70f24038201857 Mon Sep 17 00:00:00 2001 From: Costa <47926492N@ICO.SCS.local> Date: Thu, 24 Feb 2022 15:28:36 +0100 Subject: [PATCH] Adaptado para incluir Facets. --- R/ggstats_add_xy.R | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/R/ggstats_add_xy.R b/R/ggstats_add_xy.R index c699916..ea4f7c0 100644 --- a/R/ggstats_add_xy.R +++ b/R/ggstats_add_xy.R @@ -1,11 +1,19 @@ -ggstats_add_xy<-function(table_stat, table, group, y="max", bracket.offset=0.05, bracket.inspace=0.05){ +ggstats_add_xy<-function(table_stat, table, group, xcol=NULL, y="max", bracket.offset=0.05, bracket.inspace=0.05, exclude_group=NULL){ ## Adapted version to fit rstatix output value.var<-table_stat[[1,".y."]] - x<-colnames(table_stat)[1] + if (is.null(xcol)){ + x<-colnames(table_stat)[1] + }else{x<-xcol} + table[,group]<-as.factor(table[,group]) table[,x]<-as.factor(table[,x]) + if (is.null(exclude_group)){ + table_agg<-table %>% group_by(.data[[x]]) + }else{ + table_agg<-table %>% group_by(.data[[x]], .data[[exclude_group]]) + } if (y == "max"){ - agg<-table %>% group_by(.data[[x]]) %>% summarise(max=max(.data[[value.var]])) + agg<-table_agg %>% 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)) @@ -32,10 +40,22 @@ ggstats_add_xy<-function(table_stat, table, group, y="max", bracket.offset=0.05, "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) + 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 (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]], + 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) + } } return(cbind(table_stat,t) %>% as_tibble)