add adjusted p value to ggplot with comparison

5.5k views Asked by At

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

1

There are 1 answers

3
mnm On

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