r - 将自定义注释添加到 p 值标签到 ggpubr stat_compare_means()

标签 r ggplot2 boxplot ggpubr

我尝试制作显示 p 值的箱线图

my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth, x = "dose", y = "len")+ 
  stat_compare_means(comparisons = my_comparisons, method = "wilcox.test")

结果是

enter image description here

但是如何向计算的 p 值添加额外的文本?我想添加 "p = " 如下所示

enter image description here

我怎样才能做到?

upd. 下面的变体不起作用

my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )

ggboxplot(ToothGrowth, x = "dose", y = "len")+ 
  stat_compare_means(comparisons = my_comparisons, method = "wilcox.test", aes(label=paste("p=",scientific(as.numeric(..p.format..)))))

ggboxplot(ToothGrowth, x = "dose", y = "len")+ 
  stat_compare_means(comparisons = my_comparisons, method = "wilcox.test", aes(label = paste("p =", ..p.format..)))

ggboxplot(ToothGrowth, x = "dose", y = "len")+ 
  stat_compare_means(comparisons = my_comparisons, method = "wilcox.test", aes(label = paste0("p =", ..p.format..)))

最佳答案

我相信这可能是 ggpubr 中的潜在错误。该文档提供了一个示例,说明如何将自定义文本添加到您的 p 值。当将多个比较传递给标记函数时,这开始失败。我认为该错误可能是 ggplot 不知道将哪个表达式解析为标签(请参阅下面的 r-lang 警告)。还有一些关于可能相关的 p 值计算的警告。

也就是说,我觉得大多数关于 ggpubr 的问题都与具有多个 p 值的注释有关,我确实觉得您可能需要重新考虑 (a) 您的统计数据和 (b) 您的可视化。

library(ggpubr)
#> Loading required package: ggplot2
library(tidyverse)

## This works as expected. Only one test
ggboxplot(ToothGrowth, x = "supp", y = "len") + 
  stat_compare_means(aes(label = paste0("p = ", ..p.format..)))

看到大量警告 - 开始失败。

my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth, x = "dose", y = "len") +
  stat_compare_means(comparisons = my_comparisons, 
                     aes(label = paste0("p = ", ..p.format..)))
#> Warning: Using `as.character()` on a quosure is deprecated as of rlang 0.3.0.
#> Please use `as_label()` or `as_name()` instead.
#> This warning is displayed once per session.
#> Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
#> cannot compute exact p-value with ties
#> Warning in wilcox.test.default(c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, :
#> cannot compute exact p-value with ties
#> Warning in wilcox.test.default(c(16.5, 16.5, 15.2, 17.3, 22.5, 17.3, 13.6, :
#> cannot compute exact p-value with ties

通过分别为每个比较组创建一个图,这又开始起作用了。

ls_tg <- ToothGrowth %>%
  split(., .$dose)

lapply(my_comparisons, function(x) bind_rows(ls_tg[x])) %>%
  map(~ggboxplot(., x = "dose", y = "len") + 
        stat_compare_means(aes(label = paste0("p = ", ..p.format..)))) %>%
  patchwork::wrap_plots()

reprex package 创建于 2021-12-07 (v2.0.1)

关于r - 将自定义注释添加到 p 值标签到 ggpubr stat_compare_means(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70260121/

相关文章:

r - 将引号添加到R中的向量

r - 为什么我的错误栏的末端没有显示?

r - 没有数据时如何避免geom_line或geom_path中的连接线?

r - 在beanplot{beanplot}中使用 'wd='动态改变bean的宽度

r - 在连续的x轴上按组填充和躲避箱线图

R:如何使 switch 语句失效

r - 是否可以在查看器 Pane 中查看 HTML 表格?

r - 格式化多个 geom_sf 图例

r - 如何将循环与geom_vline和facet_wrap一起使用?

r - R中有没有一种方法可以根据代表2个不同常量值的两个多边形创建一个新的多边形(代表某个常量值)