在 ggplot2 barplot 上手动绘制子组之间的显着性关系

drg*_*bon 4 r significance ggplot2 ggpubr

ggplot2我一直在尝试在条形图上为一组组的子集绘制手动标记的显着性条形图,ggsignif无论ggpubr运气如何。数据类似于以下 MWE:

set.seed(3)
## create data
df <- data.frame(activity = rep(c("Flying", "Jumping"), 3),
                 mean = rep(rnorm(6, 50, 25)),
                 group = c(rep("Ecuador", 2),
                           rep("Peru", 2),
                           rep("Brazil", 2)))
## plot it
ggplot(df, aes(x = activity, y = mean, fill = group)) +
    geom_bar(position = position_dodge(0.9), stat = "identity",
             width = 0.9, colour = "black", size = 0.1) +
    xlab("Activity") + ylab("Mean")
Run Code Online (Sandbox Code Playgroud)

示例图 我想手动指定重要性标签,例如“飞行”上的“巴西/厄瓜多尔”和“跳跃”上的厄瓜多尔/秘鲁。有谁知道如何正确处理此类数据,例如ggsignif?有没有办法通过名称引用每个条形图,而不是尝试计算出其 x 轴位置?

dc3*_*c37 6

如果您知道要在哪个条形图上添加重要性标签,您可以执行以下操作:

library(ggsignif)
library(ggplot2)

ggplot(df, aes(x = activity, y = mean, fill = group)) +
  geom_bar(position = position_dodge(0.9), stat = "identity",
           width = 0.9, colour = "black", size = 0.1) +
  xlab("Activity") + ylab("Mean")+
  geom_signif(y_position = c(60,50), xmin = c(0.7,2), xmax = c(1,2.3),
              annotation=c("**", "***"), tip_length=0)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

它回答了你的问题吗?