R语言绘图,大组中有小组
含Error bar
组件比较线段 星号
绘图效果如下:
image.png
参考数据如下:
aqy.csv
Treatment,group, dose,result
A,n,0,20413
A,n,0,15551
A,n,0,22592
A,n,2,6512
A,n,2,7082
A,n,2,5761
B,n,4,3304
B,n,4,4270
B,n,4,3432
B,n,6,1004
B,n,6,1281
B,n,6,1121
C,y,0,194638
C,y,0,187731
C,y,0,186185
C,y,2,193504
C,y,2,157346
C,y,2,192429
D,y,4,156758
D,y,4,124163
D,y,4,145171
D,y,6,87017
D,y,6,80206
D,y,6,67719
代码如下:
library(ggplot2)
library(ggpubr)
setwd("~/hss/aqy/")
data <- read.csv("aqy.csv")
ggplot(data, aes(x = factor(dose), y = result, fill = group)) +
geom_bar(stat = "summary", fun = "mean", position = position_dodge(1),
#width = 1.5,
alpha = 0.3) +
# 误差线
geom_errorbar(stat = "summary", fun.data = "mean_se",
position = position_dodge(width = 1), width = 0.2) +
# dose 0 的比较
geom_segment(x = 1-0.3, xend = 1+0.3,
y = max(data$result) * 1.1, yend = max(data$result) * 1.1,
size = 0.5) +
geom_segment(x = 1-0.3, xend = 1-0.3,
y = max(data$result) * 1.08, yend = max(data$result) * 1.1,
size = 0.5) +
geom_segment(x = 1+0.3, xend = 1+0.3,
y = max(data$result) * 1.08, yend = max(data$result) * 1.1,
size = 0.5) +
stat_compare_means(aes(group = group),
method = "t.test",
label = "p.signif",
label.y = max(data$result) * 1.15,
size = 10,
data = subset(data, dose == 0)) +
# dose 2 的比较
geom_segment(x = 2-0.3, xend = 2+0.3,
y = max(data$result) * 1.0, yend = max(data$result) * 1.0,
size = 0.5) +
geom_segment(x = 2-0.3, xend = 2-0.3,
y = max(data$result) * 0.98, yend = max(data$result) * 1.0,
size = 0.5) +
geom_segment(x = 2+0.3, xend = 2+0.3,
y = max(data$result) * 0.98, yend = max(data$result) * 1.0,
size = 0.5) +
stat_compare_means(aes(group = group),
method = "t.test",
label = "p.signif",
label.y = max(data$result) * 1.05,
size = 10,
data = subset(data, dose == 2)) +
# dose 4 的比较
geom_segment(x = 3-0.3, xend = 3+0.3,
y = max(data$result) * 0.9, yend = max(data$result) * 0.9,
size = 0.5) +
geom_segment(x = 3-0.3, xend = 3-0.3,
y = max(data$result) * 0.88, yend = max(data$result) * 0.9,
size = 0.5) +
geom_segment(x = 3+0.3, xend = 3+0.3,
y = max(data$result) * 0.88, yend = max(data$result) * 0.9,
size = 0.5) +
stat_compare_means(aes(group = group),
method = "t.test",
label = "p.signif",
label.y = max(data$result) * 0.95,
size = 10,
data = subset(data, dose == 4)) +
# dose 6 的比较
geom_segment(x = 4-0.3, xend = 4+0.3,
y = max(data$result) * 0.8, yend = max(data$result) * 0.8,
size = 0.5) +
geom_segment(x = 4-0.3, xend = 4-0.3,
y = max(data$result) * 0.78, yend = max(data$result) * 0.8,
size = 0.5) +
geom_segment(x = 4+0.3, xend = 4+0.3,
y = max(data$result) * 0.78, yend = max(data$result) * 0.8,
size = 0.5) +
stat_compare_means(aes(group = group),
method = "t.test",
label = "p.signif",
label.y = max(data$result) * 0.85,
size = 10,
data = subset(data, dose == 6)) +
# 添加 dose 0 和 2 的 n 组之间的比较
geom_segment(x = 1-0.3, xend = 2-0.3,
y = max(data$result) * 0.7, yend = max(data$result) * 0.7,
size = 0.5) +
geom_segment(x = 1-0.3, xend = 1-0.3,
y = max(data$result) * 0.68, yend = max(data$result) * 0.7,
size = 0.5) +
geom_segment(x = 2-0.3, xend = 2-0.3,
y = max(data$result) * 0.68, yend = max(data$result) * 0.7,
size = 0.5) +
stat_compare_means(data = subset(data, group == "n" & dose %in% c(0, 2)),
aes(group = factor(dose)),
method = "t.test",
label = "p.signif",
label.y = max(data$result) * 0.75,
size = 10) +
labs(x = "Dose",
y = "Result",
fill = "Group") +
theme_minimal() +
theme(
panel.grid = element_blank(),
axis.line = element_line(color = "black"),
text = element_text(color = "black"),
axis.text = element_text(color = "black", size = 20),
axis.title = element_text(color = "black", size = 26),
legend.text = element_text(color = "black", size = 20),
legend.title = element_text(color = "black", size = 22)
) +
scale_fill_manual(values = c("n" = "#644296", "y" = "#D1352C")) +
scale_y_continuous(expand = c(0, 0),
limits = c(0, max(data$result) * 1.3))