Adaptada la función para permitir comparaciones entre elementos de la x, no sólo agrupados.
This commit is contained in:
+18
-6
@@ -1,11 +1,11 @@
|
|||||||
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_test<-function(table_stat, table, xcol=NULL, group, y="max", bracket.offset=0.05, bracket.inspace=0.05, exclude_group=NULL){
|
||||||
## 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(table[,group])}
|
||||||
table[,x]<-as.factor(table[,x])
|
table[,x]<-as.factor(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]])
|
||||||
@@ -14,6 +14,7 @@ ggstats_add_xy<-function(table_stat, table, group, xcol=NULL, y="max", bracket.o
|
|||||||
}
|
}
|
||||||
if (y == "max"){
|
if (y == "max"){
|
||||||
agg<-table_agg %>% summarise(max=max(.data[[value.var]]))
|
agg<-table_agg %>% summarise(max=max(.data[[value.var]]))
|
||||||
|
if(!is.na(exclude_group)){agg<-table_agg %>% group_by(.data[[exclude_group]]) %>% summarise(max=max(.data[[value.var]]))}
|
||||||
}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]])) %>% 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))
|
||||||
@@ -30,8 +31,13 @@ ggstats_add_xy<-function(table_stat, table, group, xcol=NULL, y="max", bracket.o
|
|||||||
|
|
||||||
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}
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
if(!is.null(group)){
|
||||||
x.index<-sapply(table_stat %>% pull(x), function(y) which(levels(table_stat %>% pull(x)) == y))
|
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,
|
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,
|
||||||
@@ -39,7 +45,13 @@ ggstats_add_xy<-function(table_stat, table, group, xcol=NULL, y="max", bracket.o
|
|||||||
"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)),
|
"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)
|
"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")
|
) %>% 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)){
|
if (!is.null(exclude_group)){
|
||||||
for (j in unique(pull(table_stat, all_of(exclude_group)))){
|
for (j in unique(pull(table_stat, all_of(exclude_group)))){
|
||||||
for (dia in unique(pull(table_stat,all_of(xcol)))){
|
for (dia in unique(pull(table_stat,all_of(xcol)))){
|
||||||
@@ -57,6 +69,6 @@ ggstats_add_xy<-function(table_stat, table, group, xcol=NULL, y="max", bracket.o
|
|||||||
by=diff(range(table[,value.var], na.rm = T))*bracket.inspace)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user