Combining stat_pvalue_manual with fill = GROUP

Viewed 445

Hello I am trying to use stat_pvalue_manual from the ggpubr package to show the output of some emmeans and Tukey's post hoc tests on my graphs. I have used stat_pvalue_manual successfully in the past, but never with a fill = GROUP attribute. This seems to cause some issues. The packages needed to replicate this example are tidyverse, ggpubr, rstatix, and emmeans.

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
emmeans_tooth <- ToothGrowth %>%
  group_by(supp) %>%
  emmeans_test(len ~ dose, p.adjust.method = "bonferroni")
ggplot(data = ToothGrowth, mapping = aes(x = supp, y = len, fill = dose)) +
  geom_bar(stat = "summary", fun = "mean", position = "dodge") +
  stat_pvalue_manual(data = emmeans_tooth, label = "{p.adj.signif}", y.position = c(30, 33, 25, 35, 38), hide.ns = TRUE)

This structure has worked in the past, but now that there's a fill aes, it returns an error "Error in FUN(X[[i]], ...) : object 'dose' not found". Not sure what's going on there.

I thought maybe this was due to the emmeans output, since I haven't used that before. But it doesn't work with Tukey's either. I also tried moving the mapping = aes to the geom to avoid stat_pvalue_manual from inheriting those aesthetics.

tension_L <- warpbreaks %>%
  filter(tension == "L")
tension_L_aov <- aov(breaks ~ wool, data = tension_L)
summary(tension_L_aov)
tukey_breaks <- tukey_hsd(tension_L_aov)
ggplot(data = warpbreaks) +
  geom_boxplot(mapping = aes(x = tension, y = breaks, fill = wool)) +
  stat_pvalue_manual(tukey_breaks, label = "{p.adj.signif}", y.position = 30, hide.ns = TRUE)

It then successfully makes a graph but adds the significance bracket in it's own region of the x axis.

This works for me but it's using t_tests, so it's not really the correct post hocs for what I want.

stat.test <- ToothGrowth %>%
  group_by(supp) %>%
  t_test(len ~ dose) %>%
  adjust_pvalue(method = "bonferroni") %>%
  add_significance("p.adj")
stat.test <- stat.test %>%
  add_xy_position(x = "supp", dodge = 0.8)
stat.test
bxp <- ggboxplot(ToothGrowth, x = "supp", y = "len", color = "dose", palette = c("red", "blue", "green")) +
  stat_pvalue_manual(stat.test,  label = "p", tip.length = 0)
bxp

Not sure what I need to do differently. The rationale for why I want to do this is to show significant post hocs of two way between subject ANOVAs (either with emmeans from simple main effects or Tukey's). Can anyone help me out?

0 Answers
Related