How to add p-values in form of post hoc test brackets to grouped boxplots?

105 views Asked by At

This is the plot I am working on:

.

I plotted my data depending on two parameters (treatment & crown.layer) with ggplot and am now aiming to add p-values that show significant differences within and among groups of these parameters. Therefore I want to determine a global Anova p-value and perform a post hoc test for comparing all groups pairwise. I applied the code as presented in this link.

This is my code: I am using two different datasets for running the tests and the plot, because the test didn't work with the aggregated dataset.

stat.test <- rel_data %>%
  group_by(treatment) %>%
  t_test(rel_fruit ~ crown.layer) %>%
  adjust_pvalue(method = "bonferroni") %>%
  add_significance("p.adj")%>%
  add_xy_position(x = "treatment", dodge = 0.8)

stat.test2 <- rel_data %>%
  t_test(rel_fruit ~ treatment, p.adjust.method = "bonferroni")%>% 
  add_xy_position(x = "treatment")

ggplot(grouped_data, aes(x=treatment, y=mean_fruit_b, fill=crown.layer)) +
  geom_boxplot()+ 
  geom_jitter()+
  labs( x = "treatment", y = "mean fruit to closed bud ratio [%]",title = "Fruit Development")+
  theme_light()+
  stat_pvalue_manual(stat.test,  label = "p", tip.length = 0)+  
  stat_pvalue_manual(stat.test2,  label = "p", tip.length = 0.02,step.increase = 0.05) +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))+
  scale_fill_manual (values = c("lower"="#b0e0e6","middle"="lightpink","upper"="lightgoldenrod1"))

Somehow I am receiving the following error message...

Error:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error:
! Objekt 'crown.layer' nicht gefunden
Backtrace:
  1. base (local) `<fn>`(x)
  2. ggplot2:::print.ggplot(x)
  4. ggplot2:::ggplot_build.ggplot(x)
  5. ggplot2:::by_layer(...)
 12. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
 13. l$compute_aesthetics(d, plot)
 14. ggplot2 (local) compute_aesthetics(..., self = self)
 15. base::lapply(aesthetics, eval_tidy, data = data, env = env)
 16. rlang (local) FUN(X[[i]], ...)
1

There are 1 answers

1
shizzle On

crown.layer variable you use in ggplot(grouped_data, aes(x=treatment, y=mean_fruit_b, fill=crown.layer)) does not seem to be in your grouped_data dataset

So you should add it:

grouped_data$crown.layer <- rel_data$crown.layer

(if it is in the rel_data dataset)