在 R 中的 ggplot 中,Transform Stat compare means function to p values less than or greater than 0.05

Transform Stat compare means function to p values less or greater that 0.05 in ggplot in R

如何转换 stat_compare_means 函数,使其在图表上显示 p 值 > 或 < 0.05,而不是数字或星号?

目前它只显示数字而不是 < 或 > 0.05

gg.plot <- ggplot(df, aes(x=Group, y=Lev, alpha==Group, fill=Group)) +
  geom_boxplot() +
  scale_fill_manual(values=c('#D32F2F','#3F51B5'))

gg.plot <- gg.plot + stat_compare_means(aes(label = format.pval(..p.adj.., digits = 3), label =  "p.signif", label.x = 1.5, label.y = 49)

您可以使用 stat_compare_means 中的 symnum.args 参数定义自己的标签。设置您的切割点并添加符号或文本或您想要的任何内容。

一些可重现的数据:

set.seed(1)
Group <- c(rep("A", 50), rep("B", 50), rep("C", 50))
Lev = c(rnorm(50, 5, 2), rnorm(50, 7, 3), rnorm(50, 7, 2))
df <- data.frame(Group, Lev)  

ggplot(df, aes(x=Group, y=Lev, fill=Group)) +
  geom_boxplot()+ 
  stat_compare_means(comparisons = list(c("A", "B"), c("B", "C")),
                     method = "t.test",
                     symnum.args = list(cutpoints = c(0, 0.05, Inf), 
                                        symbols = c("p <0.05", "p >0.05")))