将显着性水平(用于百分比差异)添加到多级闪避 ggplot2

Adding significance levels (for percentage differences) to a multilevel dodge ggplot2

我正在尝试使用 ggsignif 包,并使用以下数据重现 const-ae here 给出的答案:

library(ggplot2)
library(ggsignif)
counts <- structure(list(ECOST = c("0.52", "0.52", "0.39", "0.39", "0.26", 
"0.26", "0.13", "0.13", "0.00", "0.00"), group = c("control", 
"treatment", "control", "treatment", "control", "treatment", 
"control", "treatment", "control", "treatment"), count = c(18, 
31, 30, 35, 47, 46, 66, 68, 86, 86), percentage = c(16.3636363636364, 
31.9587628865979, 27.2727272727273, 36.0824742268041, 42.7272727272727, 
47.4226804123711, 60, 70.1030927835051, 78.1818181818182, 88.659793814433
), total = c(110, 97, 110, 97, 110, 97, 110, 97, 110, 97), negative_count = c(92, 
66, 80, 62, 63, 51, 44, 29, 24, 11), p_value = c(0.00843644912924255, 
0.00843644912924255, 0.172947686684261, 0.172947686684261, 0.497952719783453, 
0.497952719783453, 0.128982570547408, 0.128982570547408, 0.0447500820026408, 
0.0447500820026408)), row.names = c(NA, -10L), class = c("data.table", 
"data.frame"))

counts %>% 
    ggplot(aes(x = ECOST, y = percentage, fill = group, label=sprintf("%.02f %%", round(percentage, digits = 1)))) + 
    geom_col(position = 'dodge') + 
    geom_text(position = position_dodge(width = .9),    # move to center of bars
              vjust = -0.5,    # nudge above top of bar
              size = 4) +           
    scale_fill_grey(start = 0.8, end = 0.5) +
    theme_bw(base_size = 15)

数据:

    ECOST     group count percentage total negative_count p_value
 1:  0.52   control    18         16   110             92  0.0084
 2:  0.52 treatment    31         32    97             66  0.0084
 3:  0.39   control    30         27   110             80  0.1729
 4:  0.39 treatment    35         36    97             62  0.1729
 5:  0.26   control    47         43   110             63  0.4980
 6:  0.26 treatment    46         47    97             51  0.4980
 7:  0.13   control    66         60   110             44  0.1290
 8:  0.13 treatment    68         70    97             29  0.1290
 9:  0.00   control    86         78   110             24  0.0448
10:  0.00 treatment    86         89    97             11  0.0448

所以下一步是将显着性水平添加到图中。我一直在弄乱它,但我对代码的理解不够好以适应it.I知道我必须覆盖情节数据,但我不知道该怎么做。

counts %>% 
    ggplot(aes(x = ECOST, y = percentage, fill = group, label=sprintf("%.02f %%", round(percentage, digits = 1)))) + 
    geom_col(position = 'dodge') + 
    geom_text(position = position_dodge(width = .9),    # move to center of bars
              vjust = -0.5,    # nudge above top of bar
              size = 4) +           
    scale_fill_grey(start = 0.8, end = 0.5) +
    theme_bw(base_size = 15) +
    geom_signif(stat="identity",
              data=data.frame(x=c(0.875, 1.875), xend=c(1.125, 2.125),
                              y=c(5.8, 8.5), annotation=c("**", "NS")),
              aes(x=x,xend=xend, y=y, yend=y, annotation=annotation)) +
    geom_signif(comparisons=list(c("treatment", "control")), annotations="***",
              y_position = 9.3, tip_length = 0, vjust=0.4)

如果有人能向我解释如何进行控制和治疗,我会非常高兴,也许是两个级别的 ECOST 之间的一个,之后我确信我可以从那里开始。

期望的结果(取自link):

我不确定 'significant level' 您想要添加到图表中的内容。不过,这可能会帮助您了解如何添加自己的内容。

counts %>% 
  ggplot(aes(x = ECOST, y = percentage, fill = group)) + 
  geom_col(position = 'dodge')  +           
  scale_fill_grey(start = 0.8, end = 0.5) +
  theme_bw(base_size = 15) +
 ## between ESCOT levels
  geom_signif(annotation=c("**", "NS"), y_position = c(95, 75), xmin=c(1,2),
              xmax=c(2,5)) +
  geom_signif(annotation=c("***", "*"), y_position = c(50, 40), xmin=c(3.75,4.75),
              xmax=c(4.25,5.25)) ## Within ESCOT levels but between groups

y_poistion 是您想要绘制的 y 值

xmin 是括号开始位置的向量,因此在示例中,** 括号从第一组柱开始,ns 括号从第二个

xmaxxmin 相同,但括号结束。

你也不需要两个 geom_signif 层,我只是把它们分成两层来显示组之间和级别之间的差异。

 + geom_signif(annotation=c("**", "NS","***", "*"), y_position = c(95, 75,50, 40), xmin=c(1,2,3.75,4.75),
            xmax=c(2,5,4.25,5.25))