【问题标题】:Correct positioning of multiple significance labels in ggplot在ggplot中正确定位多个重要标签
【发布时间】:2020-07-01 11:34:05
【问题描述】:

我想在箱线图中展示两组随时间推移的发展情况,并为每个组添加重要标签(组内比较)。我有一个使用 ggpubr::stat_compare_means 的工作示例,但我无法正确定位这两个几何图形。

我尝试了position = position_dodge(width=0.5) 和其他几个位置元素,但由于某些原因,它们根本不会移动。我想要的输出将使每个标签集水平移动以覆盖每个组框,并垂直调整为不重叠。

使用diamonds的示例代码:

df <- filter(diamonds, color == "J" | color == "E")
ggplot(data = df, aes(x = cut, y = price, fill = color)) +
  geom_boxplot() +
  stat_compare_means(method = "t.test",data = filter(df, color == "J"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium"))) +
  stat_compare_means(method = "t.test",data = filter(df, color == "E"), comparisons = list(c("Fair","Good"),c("Fair","Very Good"),c("Fair","Premium")))

【问题讨论】:

  • 也许这可以作为一个很好的激励来摆脱在情节中具有多个显着性条的多个组间和组内测试?可视化不一定会受益于显着性栏的人口过多...如果您真的想呈现这么多显着性值,我认为使用表格会更好。
  • 虽然这可能是真的,但我仍然需要生成所需的图形。只有两组标签,用于参考组的 x 轴上的组间比较。

标签: r ggplot2 ggpubr


【解决方案1】:

虽然我真的不认为这对于可视化来说是一个好主意 - 这是一个解决方案。如果您使用 ggpubr,请使用 ggpubr 语法。并使用分面进行子分组。

附:换一张桌子试试。


library(tidyverse)
library(ggpubr)

mydf <- filter(diamonds, color == "J" | color == "E")

comparisons <- list(c("Fair", "Good"), c("Fair", "Very Good"), c("Fair", "Premium"))

ggboxplot(mydf, x = "cut", y = "price", facet.by = "color") +
  stat_compare_means(
    method = "t.test", ref.group = "Fair", label = "p.format",
    comparisons = comparisons
  )

reprex package (v0.3.0) 于 2020 年 3 月 20 日创建

【讨论】:

  • 非常感谢您的解决方案以及您对多个标签的意见。关于您的解决方案,我有两个问题;是否可以在 ggplot-syntax 中执行此操作,是否可以避免分面,即使它可能会导致混乱?
  • 不幸的是,如果没有大规模的黑客攻击,我认为这是不可能的。而且我认为没有刻面 - 小插图和子组教程中的所有示例都包括刻面。它并不是真正为这类东西设计的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 2021-05-08
  • 2018-03-30
  • 1970-01-01
相关资源
最近更新 更多