【问题标题】:adding significance brackets to ridgeline plot向山脊线图添加显着性括号
【发布时间】:2021-07-29 12:52:14
【问题描述】:

我正在创建一个岭图来比较几个组(使用 ggridges 包),并希望添加显着性括号以显示某些组级别之间的比较(使用 ggsignif 包)。

但这似乎不起作用,因为 stat_ggsignif 中的计算失败。

这是一个可重现的例子:

set.seed(123)
library(ggsignif)
library(ggridges)
library(ggplot2)

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(scale = 1) +
  coord_flip() + 
  geom_signif(comparisons = list(c("setosa", "versicolor")))
#> Picking joint bandwidth of 0.181
#> Warning in f(..., self = self): NAs introduced by coercion
#> Warning: Computation failed in `stat_signif()`:
#> missing value where TRUE/FALSE needed

reprex package (v2.0.0) 于 2021 年 7 月 29 日创建

如何让这两个软件包相互配合?谢谢。

【问题讨论】:

  • 我同意 @ventrilocus 的观点,即 ggridges 可能不是合适的软件包。检查gghalves::geom_half_violin 和/或see::geom_violinhalf

标签: r ggplot2


【解决方案1】:

我没有设法将 A) geom_density_ridges 和 B) geom_signif 结合起来。原因是 (A) 要求数值变量为 x,类别为 y,而 (B) 要求数值变量为 y,类别为 x。而且我还没有设法覆盖这种行为。 但我假设您选择了 ridge_plots 而不是简单的箱线图,因为您对分布的信息更丰富的可视化感兴趣。为此,有一个比 ridge_plots 更好的解决方案,即所谓的小提琴图。请参阅下面的标准箱线图(带有标记的重要性):

ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + 
  geom_boxplot() + 
  geom_signif(comparisons = list(c("setosa", "versicolor")), test = "t.test")

参见下面的小提琴图(带有抖动和标记的重要性):

ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + 
  geom_violin(trim = F) + geom_jitter() +
  geom_signif(comparisons = list(c("setosa", "versicolor")), test = "t.test")

除非您对使 ggridges 和 ggsignif 一起工作特别感兴趣,否则这可以完成工作。请注意,小提琴图只是一个折叠密度图(有关详细信息,请参阅https://en.wikipedia.org/wiki/Violin_plot#:~:text=A%20violin%20plot%20is%20a,by%20a%20kernel%20density%20estimator)。

出于同样的目的,另请参见 sina 图(tjebo 的建议):

library(ggforce)
ggplot(iris, aes(x = Species, y = Sepal.Length, colour = Species)) + 
  geom_sina() +
  geom_signif(comparisons = list(c("setosa", "versicolor")), test = "t.test")

【讨论】:

  • 如果您希望将抖动和小提琴图结合起来,可能有一个更巧妙的解决方案 - "Sina plot"。检查ggforce::geom_sina,或本质上非常相似,ggbeeswarm::geom_quasirandom
  • 不错的建议。我已将其添加到答案中
  • @Ventrilocus 感谢您的详细回复!如果没有其他建议,我将等待一天左右,然后接受您的回答。
【解决方案2】:

感谢ggsignif 的新拉取请求,以下内容现在可以使用:

set.seed(123)
library(ggsignif)
library(ggridges)
library(ggplot2)

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(scale = 1) +
  coord_flip() + 
  geom_signif(comparisons = list(c("setosa", "versicolor")),
              y_position = 9)
#> Picking joint bandwidth of 0.181

reprex package (v2.0.1) 于 2021-08-06 创建

【讨论】:

    猜你喜欢
    • 2022-06-14
    • 2014-08-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多