求助:基于R语言为activity和task_persistence创建堆叠分组条形图
R语言实现带误差区间的分组堆叠条形图
1. 数据预处理
原数据框中的数值和误差区间均为字符格式,需先转换为数值型并拆分误差区间上下限:
library(tidyverse) library(patchwork) # 加载原始数据 rG_activity <- c("0.368", "0.468", "0.427", "0.091") rG_activity_error_intervals <- c("(0.468, 0.268)", "(0.568, 0.368)", "(0.527, 0.327)", "(1.091, -0.909)") rG_task_persistence <- c("-0.462", "-0.345", "-0.291", "-1.062") rG_task_persistence_error_intervals <- c("(-0.562, -0.362)", "(-0.445, -0.245)", "(-0.391, -0.191)", "(-2.091, -0.062)") rE_activity <- c("-0.162", "-0.245", "-0.129", "-0.462") rE_activity_error_intervals <- c("(-0.262, -0.062)", "(-0.345, -0.145)", "(-0.229, -0.029)", "(-0.562, -0.362)") rE_task_persistence <- c("-0.362", "-0.245", "-0.191", "-0.162") rE_task_persistence_error_intervals <- c("(-0.462, -0.262)", "(-0.345, -0.145)", "(-0.291, -0.091)", "(-0.262, -0.062)") age <- c("30", "24", "18", "12") df <- data.frame(age, rG_activity, rG_activity_error_intervals, rG_task_persistence, rG_task_persistence_error_intervals, rE_activity, rE_activity_error_intervals, rE_task_persistence, rE_task_persistence_error_intervals) # 清洗数据:转换数值类型、拆分误差区间 df_clean <- df %>% mutate(across(starts_with("r"), as.numeric)) %>% # 拆分activity类误差区间 separate(rG_activity_error_intervals, into = c("rG_activity_upper", "rG_activity_lower"), sep = ", ", remove = TRUE) %>% separate(rE_activity_error_intervals, into = c("rE_activity_upper", "rE_activity_lower"), sep = ", ", remove = TRUE) %>% # 拆分task_persistence类误差区间 separate(rG_task_persistence_error_intervals, into = c("rG_task_upper", "rG_task_lower"), sep = ", ", remove = TRUE) %>% separate(rE_task_persistence_error_intervals, into = c("rE_task_upper", "rE_task_lower"), sep = ", ", remove = TRUE) %>% # 去掉括号并转换为数值 mutate(across(ends_with(c("upper", "lower")), ~ str_remove_all(., "\\(|\\)") %>% as.numeric())) %>% # 设置年龄为有序因子,保证绘图顺序正确 mutate(age = factor(age, levels = c("12", "18", "24", "30")))
2. 数据重塑(宽转长)
将宽格式数据转换为ggplot2适配的长格式,区分指标类型与分组:
df_long <- df_clean %>% pivot_longer( cols = -age, names_to = c("group", "indicator", "error_type"), names_pattern = "(rG|rE)_(activity|task_persistence)(_upper|_lower)?", values_to = "value" ) %>% mutate( error_type = ifelse(is.na(error_type), "estimate", error_type), indicator = factor(indicator, levels = c("activity", "task_persistence")) ) %>% pivot_wider( names_from = error_type, values_from = value )
3. 绘制上下堆叠的分组条形图
用ggplot2分别绘制两个指标的带误差区间分组条形图,再通过patchwork拼接,将图例统一置于底部:
# 定义统一绘图主题 plot_theme <- theme_minimal() + theme( legend.position = "bottom", legend.title = element_text(size = 10), axis.title.x = element_text(size = 11), axis.title.y = element_text(size = 11), plot.title = element_text(size = 12, hjust = 0.5) ) # 绘制Activity指标图 plot_activity <- df_long %>% filter(indicator == "activity") %>% ggplot(aes(x = age, y = estimate, fill = group)) + geom_col(position = position_dodge(width = 0.8), width = 0.7) + geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(width = 0.8), width = 0.2) + labs(title = "Activity指标", x = "年龄", y = "数值") + plot_theme # 绘制Task Persistence指标图 plot_task <- df_long %>% filter(indicator == "task_persistence") %>% ggplot(aes(x = age, y = estimate, fill = group)) + geom_col(position = position_dodge(width = 0.8), width = 0.7) + geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(width = 0.8), width = 0.2) + labs(title = "Task Persistence指标", x = "年龄", y = "数值") + plot_theme # 拼接图形并合并图例 combined_plot <- plot_activity / plot_task + plot_layout(guides = "collect") & theme(legend.position = "bottom", legend.title = element_text(label = "分组")) # 显示最终图形 print(combined_plot)
关键说明
- 数据预处理:确保数值类型正确,拆分误差区间并转换为数值,有序因子保证年龄顺序符合逻辑。
- 数据重塑:通过
pivot系列函数将宽数据转为长数据,方便按指标和分组筛选绘图。 - 绘图细节:
position_dodge实现分组条形效果,geom_errorbar添加误差区间;patchwork的/操作符实现上下堆叠,plot_layout(guides = "collect")合并图例并置于底部。
内容的提问来源于stack exchange,提问作者wooden05
相关产品推荐
相关产品推荐

