Reppo for internal functions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
3.7 KiB

  1. ggstats_add_xy<-function(table_stat, table, group, xcol=NULL, y="max", bracket.offset=0.05, bracket.inspace=0.05, exclude_group=NULL){
  2. ## Adapted version to fit rstatix output
  3. value.var<-table_stat[[1,".y."]]
  4. if (is.null(xcol)){
  5. x<-colnames(table_stat)[1]
  6. }else{x<-xcol}
  7. table[,group]<-as.factor(table[,group])
  8. table[,x]<-as.factor(table[,x])
  9. if (is.null(exclude_group)){
  10. table_agg<-table %>% group_by(.data[[x]])
  11. }else{
  12. table_agg<-table %>% group_by(.data[[x]], .data[[exclude_group]])
  13. }
  14. if (y == "max"){
  15. agg<-table_agg %>% summarise(max=max(.data[[value.var]]))
  16. }else if (y == "mean"){
  17. agg<-table %>% group_by(.data[[x]],.data[[group]]) %>% summarise(mean=mean(.data[[value.var]])) %>% spread(group, mean)
  18. agg<- data.frame(x=agg[,1], "max"=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
  19. colnames(agg)[1]<-x
  20. }else if (y == "mean+sd"){
  21. agg<-table %>% group_by(.data[[x]],.data[[group]]) %>% summarise(mean=mean(.data[[value.var]])+sd(.[[value.var]])) %>% spread(group, mean)
  22. agg<- data.frame(x=agg[,1], "max"=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
  23. colnames(agg)[1]<-x
  24. }else if (y == "mean+sem"){
  25. agg<-table %>% group_by(.data[[x]],.data[[group]]) %>% summarise(mean=mean(.data[[value.var]])+sem(.[[value.var]])) %>% spread(group, mean)
  26. agg<- data.frame(x=agg[,1], "max"=apply(agg[,2:ncol(agg)], 1, max, na.rm=T))
  27. colnames(agg)[1]<-x
  28. }
  29. group.list<-list()
  30. count<-1
  31. table_stat<-mutate(table_stat, {{x}}:=as.factor(.data[[x]]))
  32. 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}
  33. x.index<-sapply(table_stat %>% pull(x), function(y) which(levels(table_stat %>% pull(x)) == y))
  34. t<-tibble("y.position"=merge(table_stat, agg ,sort=F)[,"max"]+diff(range(table[value.var], na.rm = T))*bracket.offset,
  35. "groups"=group.list,
  36. "x.temp"=x.index,
  37. "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)),
  38. "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)
  39. ) %>% rename("x"="x.temp")
  40. if (!is.null(exclude_group)){
  41. for (j in unique(pull(table_stat, all_of(exclude_group)))){
  42. for (dia in unique(pull(table_stat,all_of(xcol)))){
  43. if (stat.test %>% filter(p < 0.05) %>% filter(.data[[x]] == dia & .data[[exclude_group]] == j) %>% nrow() > 0){
  44. 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]],
  45. 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),
  46. by=diff(range(table[,value.var], na.rm = T))*bracket.inspace)
  47. }
  48. }
  49. }
  50. }else{
  51. for (dia in unique(pull(table_stat,all_of(xcol)))){
  52. t[table_stat[,x] == dia,"y.position"]<-seq(t[table_stat[,x] == dia,"y.position"][[1,1]],
  53. 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),
  54. by=diff(range(table[,value.var], na.rm = T))*bracket.inspace)
  55. }
  56. }
  57. return(cbind(table_stat,t) %>% as_tibble)
  58. }