使用 ggplot 时,如何用字符串替换 p 值?

When using ggplot, how can I replace the p-value by a string?

我需要为两组绘制箱线图,我想将 p 值添加到图中,如下所示:

df <- data.frame(group = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"),
             value = c(1:10))

ggplot(data = df,
   mapping = aes(x = group, y = value)) +
geom_boxplot() +
stat_compare_means()

但是,我不想写超级准确的 p 值,而是只写“p<0.05”。是否可以使用 stat_compare_means() 中的标签参数来做到这一点?还是需要使用注释功能?

此外,是否可以这样做:如果 p<0.05 则写“p<0.05”,否则写“n.s。”?

感谢您的帮助!!

这可以通过设置 label="p.singif" 并通过参数设置所需的出点和标签来实现 symnum.args:

library(ggpubr)
#> Loading required package: ggplot2

df <- data.frame(group = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"),
                 value = c(1:10))

ggplot(data = df,
       mapping = aes(x = group, y = value)) +
  geom_boxplot() +
  stat_compare_means(label = "p.signif", 
                     symnum.args = list(cutpoints = c(0, 0.05, 1), 
                                        symbols = c("p<0.05", "ns")))