|
@ -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) |
|
|
|
|
|
} |