ggplot2:如何在分组条形图上添加线条和 p 值?

ggplot2: how to add lines and p-values on a grouped barplot?

我尝试阅读这些帖子的答案来解决我的问题,但没有成功: Indicating the statistically significant difference in bar graph USING R, Put stars on ggplot barplots and boxplots - to indicate the level of significance (p-value).

我想添加一些线条和标签,以显示使用 R 的分组条形图中的显着性级别,就像红色矩形内的那些。

这是我编写的代码的更简单版本:

#### DATA
g <- as.factor(c('Kit1_A', 'Kit2_A', 'Kit1_B', 'Kit2_B','Kit1_C', 'Kit2_C'))
groups  <- rep(g, 3)
targets <- c(rep('X', 6), rep('Y', 6), rep('Z', 6))
mean <- c(20.8, 23.8, 21.61667, 23.54583, 22.26250, 25.41250, 20.39583, 23.82917, 20.70000, 23.82917, 21.52083, 24.83333, 20.68750, 24.60000, 20.78750, 24.42083, 22.86667, 25.28750)
sd <- c(1.249251, 1.137451, 2.372480, 2.439704, 2.149715, 1.465997, 1.579936, 0.944777, 2.320555, 1.419932, 2.636766, 2.820217, 2.014647, 1.384187, 2.193378, 1.685869, 3.456228, 2.197052)
df  <-data.frame(groups, targets, mean, sd)

#### Barplot
library(ggplot2)
f <- ggplot(df, aes(x=targets, y=mean, fill=groups))
f <- f + geom_bar(position="dodge", stat="identity", colour='black')
f <- f + geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.2,position=position_dodge(.9))
f <- f + theme(legend.title = element_blank())
f <- f + scale_fill_manual(values=c('#D6EAF8','#5DADE2','#2874A6','#D5F5E3','#58D68D','#239B56'))
f <- f + coord_cartesian(ylim = c(0, 35))

感谢您的帮助。

也许这不是最佳答案,但您可以使用 annotate("rect")/annotate("segment")grid.text 或者另一个选项 annotation_custom(grob = linesGrob())

我将你的数据框与 annotate("rect")grid.text 一起使用。

更新后的代码为:

# Add rectangles 
f + annotate("rect", xmin = 0.6, xmax = 1.07, ymin = 27, ymax =27, alpha=1,colour = "black")+
    annotate("rect", xmin = 0.6, xmax = 0.6, ymin = 26.7, ymax =27, alpha=1, colour = "black")+
    annotate("rect", xmin = 1.07, xmax = 1.07, ymin = 26.7, ymax =27, alpha=1, colour = "black")+
    annotate("rect", xmin = 0.778, xmax = 1.2, ymin = 29.5, ymax =29.5, alpha=1,colour = "black") +
    annotate("rect", xmin = 0.778, xmax = 0.778, ymin = 29.2, ymax =29.5, alpha=1,colour = "black") +
    annotate("rect", xmin = 1.2, xmax = 1.2, ymin = 29.2, ymax =29.5, alpha=1,colour = "black") +
    annotate("rect", xmin = 0.925, xmax = 1.4, ymin = 32, ymax =32, alpha=1,colour = "black") +
    annotate("rect", xmin = 0.925, xmax = 0.925, ymin = 31.5, ymax =32, alpha=1,colour = "black") +
    annotate("rect", xmin = 1.4, xmax = 1.4, ymin = 31.5, ymax =32, alpha=1,colour = "black") +

  # Second two lines
    annotate("rect", xmin = 1.61, xmax = 2.08, ymin = 27, ymax =27, alpha=1,colour = "black")+
    annotate("rect", xmin = 1.61, xmax = 1.61, ymin = 26.7, ymax =27, alpha=1, colour = "black")+
    annotate("rect", xmin = 2.08, xmax = 2.08, ymin = 26.7, ymax =27, alpha=1, colour = "black")+
    annotate("rect", xmin = 1.76, xmax = 2.2, ymin = 29.5, ymax =29.5, alpha=1,colour = "black") +
    annotate("rect", xmin = 1.76, xmax = 1.76, ymin = 29.2, ymax =29.5, alpha=1,colour = "black") +
    annotate("rect", xmin = 2.2, xmax = 2.2, ymin = 29.2, ymax =29.5, alpha=1,colour = "black")


# Add text   

  grid.text((paste("p<0.001")),
              x = unit(0.15, "npc"), y = unit(0.77, "npc"), just = c("left", "bottom"), 
              gp = gpar(fontface = "bold", fontsize = 8, col = "black"))

  grid.text((paste("p<0.001")),
              x = unit(0.185, "npc"), y = unit(0.839, "npc"), just = c("left", "bottom"), 
              gp = gpar(fontface = "bold", fontsize = 8, col = "black"))

  grid.text((paste("p<0.001")),
              x = unit(0.233, "npc"), y = unit(0.902, "npc"), just = c("left", "bottom"), 
              gp = gpar(fontface = "bold", fontsize = 8, col = "black"))
  # Second two lines
  grid.text((paste("p<0.001")),
              x = unit(0.42, "npc"), y = unit(0.77, "npc"), just = c("left", "bottom"), 
              gp = gpar(fontface = "bold", fontsize = 8, col = "black"))

  grid.text((paste("p<0.001")),
            x = unit(0.45, "npc"), y = unit(0.839, "npc"), just = c("left", "bottom"), 
            gp = gpar(fontface = "bold", fontsize = 8, col = "black"))

以及输出: