4

I need help to add the adjusted p value (bonferroni for example) on ggplot boxplot instead of p value. I've try to do it with stat_compare_means from ggpub package by using ..p.adj.. on the aesthetics, but it doesn't work when I add the comparison list.

ggplot(data= mydf, aes(x=B,y=A)) + 
  geom_boxplot() + 
  stat_compare_means(aes(label=..p.adj..),
                     comparisons = list(c("x","y"),c("x","z"),c("y","z")))

boxplot

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
zercchi
  • 41
  • 1
  • 2

1 Answers1

0

How about this;

library(ggplot2)
library(ggpubr)
mydf <- data.frame(A=1:300, B=rep(c("x","y","z"),100))
my_comparisons<- list(c("x","y"),c("x","z"),c("y","z"))

p<- ggplot(data= mydf, aes(x=B,y=A)) + geom_boxplot() +
  theme_bw()+
  stat_compare_means(aes(label=..p.adj..), comparisons = my_comparisons,
                     label.x = 1.5, label.y = 300)
#  Add p-value
p + stat_compare_means(label.y = 280, label.x = 1.2)

plot1

# Change method
p + stat_compare_means(method = "anova", label.y = 260, label.x = 2.2)

plot2

I also recommend the you look at this post on R-bloggers

[1]: https://i.stack.imgur.com/6UGw4.png
[2]: https://i.stack.imgur.com/oriH8.png
mnm
  • 1,962
  • 4
  • 19
  • 46
  • 1
    Thanks for your answer but the adjusted p value for multiple comparisons is not displayed when I add the `comparison` parameter in `stat_compare_means`. I'll use an other example. `data("ToothGrowth")` `my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )` `ggboxplot(ToothGrowth, x = "dose", y = "len", color = "dose", palette = "jco") + stat_compare_means(aes(label=..p.adj..), comparisons = my_comparisons)` – zercchi Sep 10 '17 at 08:03
  • @zercchi have you seen the help manual of `?stat_compare_means`. In it the `label` option states, `character string specifying label type. Allowed values include "p.signif" (shows the significance levels), "p.format" (shows the formatted p value).` I think, you need `compare_means()` for it has the `p.adj` option that you are looking for. – mnm Sep 10 '17 at 11:00
  • 1
    @Ashish: as you pointed out in your comment, your answer is wrong ["stat_compare_means(aes(label=..p.adj..)..." is not possible], so please correct it otherwise it is confusing. – matmar Dec 20 '17 at 19:27