【问题标题】:Plotting adjusted Bonferroni values on a ggplot在 ggplot 上绘制调整后的 Bonferroni 值
【发布时间】:2021-12-31 21:24:41
【问题描述】:

我想在显示比较条的 ggplot 中使用 Bonferroni 调整的 p 值,但我似乎无法弄清楚。

如果我使用t.test 方法...

ggplot(iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() + # using `ggsignif` to display comparison of interest
  geom_signif(
    comparisons = list(c("versicolor", "virginica"),c('versicolor','setosa'), c('virginica', 'setosa')),
    map_signif_level = TRUE,
    test = t.test,
    step_increase = .1,
    margin_top = 0.1,
    vjust = 0.5,
    textsize = 3)

...那么我无法添加 bonferroni 校正。

如果我改用 t_test 方法(其中确实包含实现 bonferroni 的方法)...

ggplot(iris, aes(x = Species, y = Sepal.Length)) +
  geom_boxplot() + # using `ggsignif` to display comparison of interest
  geom_signif(
    comparisons = list(c("versicolor", "virginica"),c('versicolor','setosa'), c('virginica', 'setosa')),
    map_signif_level = TRUE,
    test = t_test, # This method doesn't work inside geom_signif()
    p.adjust.method = "bonferroni" 
    step_increase = .1,
    margin_top = 0.1,
    vjust = 0.5,
    textsize = 3)

...它根本不起作用,因为t_test 在这种情况下不起作用。

关于如何在 ggplot 中包含经过 bonferroni 调整的 p 值的任何建议?

TIA

【问题讨论】:

  • 真的必须是ggplot2吗?我可以提供使用低级图形的解决方案。
  • 也许不是,但在 ggplot 中维护会更容易,因为我的大多数工作场所都熟悉这一点。如果它足够简单以至于我们可以维护它,我愿意接受基本 R 解决方案。

标签: r ggplot2 bonferroni


【解决方案1】:

这是一个基本的 R 解决方案。首先,在组变量上做一个pairwise.t.test的值变量,同时指定调整的方法。

p1 <- with(iris2, pairwise.t.test(Sepal.Length, Species, 
                                  p.adjust.method="bonferroni"))$p.value

然后创建一个boxplot() 并在mapply 中使用arrows() 作为范围,使用text 在其上放置一些东西,例如p-值。这是一个相当硬编码的示例;逻辑如下:x-values* 位于12、...n 级别(在这种情况下为"Species"),y-values* 位于“上方”框,其上限为max(Sepal.Length)x, y 值被放入向量中。

*有关详细信息,请参阅文档?arrows?text

ylim. <- with(iris, range(Sepal.Length)) + c(0, 1)  ## define ylim

boxplot(Sepal.Length ~ Species, iris2, ylim=ylim.)
mapply(\(w, x, y, z) arrows(w, x, y, z, code=3, angle=90, length=.05),
       c(1, 2, 1), c(7.75, 8, 8.25), c(2, 3, 3), c(7.75, 8, 8.25))
mapply(\(x, y, z) text(x, y, bquote(italic(p)*'='*.(z)), cex=.7),
       c(1.5, 2, 2.5), c(7.85, 8.35, 8.1), formatC(p1[!is.na(p1)], 3, fo='f'))

注意:使用 R >= 4.1。

给予:

如果你更喜欢重要的星星,你可以尝试以下,

c('', '*', '**', '***')[rowSums(outer(p1[!is.na(p1)], 
                                      c(1, .05, .01, .001), `<`))]

并将其作为z 放在text-mapply 中。


数据:

set.seed(42)
iris2 <- iris[sample(nrow(iris), 21), ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2020-08-04
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多