gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:33:20 25 4
gpt4 key购买 nike

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

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

plot

这是我提出的代码的更简单版本:

#### 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"))

和输出:
enter image description here

关于r - ggplot2:如何在分组条形图上添加线条和 p 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37733800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com