如何用R绘制带误差线及原始数据可视化的分组折线图
需求与解决方案
想要绘制带误差线的跨分组折线图,同时叠加原始数据可视化(如小提琴图、雨云图),弥补SD/SE/CI等汇总统计无法呈现完整数据分布的缺陷。以下基于你提供的代码,给出三种实现方式:
方式1:折线图叠加小提琴图
利用原始数据d1绘制小提琴图,通过position_dodge保证分组与折线图对齐,先画小提琴图作为底层,再叠加折线和误差线:
library(dplyr) library(ggplot2) # 生成数据 d1 <- data.frame(time_serie = as.factor(rep(rep(1:3, each = 6), 3)), treatment = as.factor(rep(c("HIGH", "MEDIUM", "LOW"), each = 18)), value = runif(54, 1, 10)) # 计算汇总统计 d2 <- d1 %>% group_by(time_serie,treatment) %>% summarise(mean_value = mean(value), sd_value = sd(value)) %>% ungroup() # 绘图:小提琴图+折线误差线 ggplot() + # 底层小提琴图,position_dodge与后续图层保持一致 geom_violin(data = d1, aes(x = time_serie, y = value, fill = treatment), position = position_dodge(0.3), alpha = 0.3) + # 误差线 geom_errorbar(data = d2, aes(x = time_serie, y = mean_value, ymin = mean_value - sd_value, ymax = mean_value + sd_value, color = treatment), width = .2, position = position_dodge(0.3), size =1) + # 均值点 geom_point(data = d2, aes(x = time_serie, y = mean_value, color = treatment), position = position_dodge(0.3), size = 3) + # 折线 geom_line(data = d2, aes(x = time_serie, y = mean_value, color = treatment, group = treatment), position=position_dodge(0.3), size =1) + theme_bw()
方式2:折线图叠加雨云图
雨云图结合了小提琴图(分布趋势)和散点(原始数据),需借助ggdist包实现,视觉信息更丰富:
library(dplyr) library(ggplot2) library(ggdist) # 数据部分同前 d1 <- data.frame(time_serie = as.factor(rep(rep(1:3, each = 6), 3)), treatment = as.factor(rep(c("HIGH", "MEDIUM", "LOW"), each = 18)), value = runif(54, 1, 10)) d2 <- d1 %>% group_by(time_serie,treatment) %>% summarise(mean_value = mean(value), sd_value = sd(value)) %>% ungroup() # 绘图:雨云图+折线误差线 ggplot() + # 雨云图(整合小提琴+散点) geom_raincloud(data = d1, aes(x = time_serie, y = value, fill = treatment, color = treatment), position = position_dodge(0.3), alpha = 0.3) + # 折线与误差线 geom_errorbar(data = d2, aes(x = time_serie, y = mean_value, ymin = mean_value - sd_value, ymax = mean_value + sd_value, color = treatment), width = .2, position = position_dodge(0.3), size =1) + geom_point(data = d2, aes(x = time_serie, y = mean_value, color = treatment), position = position_dodge(0.3), size = 3) + geom_line(data = d2, aes(x = time_serie, y = mean_value, color = treatment, group = treatment), position=position_dodge(0.3), size =1) + theme_bw()
方式3:折线图叠加抖动散点
如果追求简洁直观,直接用抖动散点展示原始数据,避免点重叠:
library(dplyr) library(ggplot2) d1 <- data.frame(time_serie = as.factor(rep(rep(1:3, each = 6), 3)), treatment = as.factor(rep(c("HIGH", "MEDIUM", "LOW"), each = 18)), value = runif(54, 1, 10)) d2 <- d1 %>% group_by(time_serie,treatment) %>% summarise(mean_value = mean(value), sd_value = sd(value)) %>% ungroup() ggplot() + # 抖动散点(避免点重叠) geom_jitter(data = d1, aes(x = time_serie, y = value, color = treatment), position = position_dodge(jitter.width = 0.2, width = 0.3), size = 2, alpha = 0.5) + # 折线与误差线 geom_errorbar(data = d2, aes(x = time_serie, y = mean_value, ymin = mean_value - sd_value, ymax = mean_value + sd_value, color = treatment), width = .2, position = position_dodge(0.3), size =1) + geom_point(data = d2, aes(x = time_serie, y = mean_value, color = treatment), position = position_dodge(0.3), size = 3) + geom_line(data = d2, aes(x = time_serie, y = mean_value, color = treatment, group = treatment), position=position_dodge(0.3), size =1) + theme_bw()
关键注意点
- 所有图层的
position_dodge(width = 0.3)数值要统一,保证分组对齐 - 原始数据图层(小提琴/散点)放在最底层,避免遮挡折线和误差线
- 可通过调整
alpha(透明度)、size(元素大小)优化视觉层次
内容的提问来源于stack exchange,提问作者Muhammad Aaqib Shamim
相关产品推荐
相关产品推荐

