ggplot 中多个重要性标签的正确定位

Correct positioning of multiple significance labels in ggplot

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

我尝试了 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")))

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

P.S。请尝试使用 table。


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 日创建