|||
前几天帮同事作一幅统计图,要求折线图下带有柱形图。折线图描绘四组指标随着时间的变化,柱形图用来给出各个时间点不同组别的显著性标记。之前一直用 SPSS 和 Excel 作图,但是两种类型的图形合成在一块似乎不太好弄。试着用 R 语言画了一下,结果还看得下去。现将代码记录如下,方便以后粘贴使用。
library(ggplot2)
graph1 <- ggplot(data = data1, aes(x = factor(day), y = mean)) +
geom_line(aes(group = group)) + #线图
geom_point(aes(shape = group), size = 3) + #点图
geom_bar(aes(y = mean/2, fill = group), colour = "black",
position = "dodge") + #柱形图
geom_errorbar(aes(ymax = mean/2 + se/2, ymin = mean/2, group = group),
width = 0.2, position = position_dodge(0.9)) + #柱形图上误差棒
geom_linerange(aes(ymax = mean + se, ymin = mean)) + #折线图上误差竖线
geom_segment(aes(x = time - 0.05, xend = time + 0.05,
y = mean + se, yend = mean + se)) + #折线图上误差横杠
geom_text(aes(y = mean/2 + se/2 + 3, label = note, group = group),
position = position_dodge(0.9),
size = 5, fontface = "bold") + #柱形图上显著性标记
scale_fill_manual(values = c("black", "#525252", "#cccccc", "white"),
labels = c("Ber Con", "Ber SE",
"Qyuan Con", "Qyuan SE")) + #填充灰色阶
scale_shape(labels = c("Ber Con", "Ber SE",
"Qyuan Con", "Qyuan SE")) + #设置点图例
scale_x_discrete(labels = paste(c(0, 3, 5, 8, 12), "day", sep = "")) +
labs(shape = "line", fill = "bar", x = NULL,
y = "Body weight (g)") + #坐标轴标签
theme_bw() + theme(panel.grid = element_blank(),
legend.text = element_text(size = 15),
axis.title = element_text(size = 20),
axis.text = element_text(size = 15),
legend.title = element_text(size = 15)) #白色主题
ggsave(plot = graph1, filename = "0graph1.jpeg", width = 34.29, height = 15.84, units = "cm")
做出来的图形如下:
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-23 05:08
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社