【问题标题】:Positioning of selected p values using ggboxplot使用 ggboxplot 定位选定的 p 值
【发布时间】:2019-10-20 12:50:45
【问题描述】:

我以 mtcars 数据集为例来运行下面的代码。

library(ggplot2)
library(ggpubr)
ggboxplot(mtcars, x = "cyl", y = "drat", fill = "cyl", 
          facet.by = "am", width = 0.5, outlier.shape = NA, 
          bxp.errorbar = TRUE, bxp.errorbar.width = 0.2) + 
  stat_compare_means(aes(label = ifelse(p < 1.e-4, 
                                        sprintf("p = %2.1e", as.numeric(..p.format..)), 
                                        sprintf("p = %5.4f", as.numeric(..p.format..)))), 
                     method = "wilcox.test", paired = FALSE)

我无法修改它以实现以下目标。在每个方面,我只想添加两个 wilcox.test p 值,一个比较 cyl=4cyl=6,另一个比较 cyl=4cyl=8。然后,我希望每个 p 值分别位于 cyl=6cyl=8 框上方。

我喜欢使用 ggsignif 包,但我需要将此示例推断为 9 次比较,这使得 ggsignif 不太合适(比较栏占用太多空间)。

感谢您的任何建议。

【问题讨论】:

  • 谢谢,我忘了添加“库”。现在添加到编辑版本中。

标签: r ggplot2 boxplot p-value ggpubr


【解决方案1】:

您希望将其他组与“参考”组cyl=4 进行比较。如果你使用 ref.group 参数到 stat_compare_means 它应该给你你想要的:

ggboxplot(mtcars, x = "cyl", y = "drat", fill = "cyl", 
          facet.by = "am", width = 0.5, outlier.shape = NA, 
          bxp.errorbar = TRUE, bxp.errorbar.width = 0.2) + 
    stat_compare_means(
        aes(label = ifelse(p < 1.e-4, 
                           sprintf("p = %2.1e", as.numeric(..p.format..)), 
                           sprintf("p = %5.4f", as.numeric(..p.format..)))), 
        ref.group = "4",
        method = "wilcox.test", paired = FALSE
    )

【讨论】:

  • 哇,太棒了!出乎意料的简单。非常感谢,马吕斯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多