通过比较将调整后的p值添加到ggplot中

时间:2017-09-08 18:56:50

标签: r ggplot2 ggpubr

我需要帮助在ggplot boxplot而不是p值上添加调整后的p值(例如bonferroni)。我已经尝试使用stat_compare_means在ggpub包中使用..p.adj..对美学进行处理,但在添加比较列表时它并不起作用。

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 个答案:

答案 0 :(得分:0)

这个怎么样;

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

我还建议您查看此post on R-bloggers

[1]: https://i.stack.imgur.com/6UGw4.png
[2]: https://i.stack.imgur.com/oriH8.png