You need to enable JavaScript to run this app. R语言基于两个同变量数据框绘制带图例与居中误差棒的柱状图
优惠活动
大模型
产品
解决方案
定价
更多

R语言基于两个同变量数据框绘制带图例与居中误差棒的柱状图

实现方案

核心调整说明

  • 完全放弃bind_rows合并数据集的操作,直接为每个几何图层单独指定对应的数据框,无需额外生成合并后的中间表
  • 所有涉及分组错位的图层(柱子、误差棒)统一使用相同宽度的position_dodge参数,确保误差棒自动居中对齐到对应柱子
  • 通过手动指定fill和color映射的标签,自动生成统一的分组图例

完整可运行代码

# 加载依赖包
library(ggplot2)
library(dplyr)

# 赋值两个原始数据框(若已在环境中可跳过)
B <- structure(list(question = c("Q1", "Q10", "Q11", "Q12", "Q2", 
"Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9"), n = c(204L, 204L, 
204L, 204L, 204L, 204L, 204L, 204L, 204L, 204L, 204L, 204L), 
    mean = c(5.22549019607843, NA, 4.95098039215686, 4.39705882352941, 
    5.47058823529412, 5.51470588235294, 4.50490196078431, 4.92647058823529, 
    4.40686274509804, 5.56862745098039, 5.56372549019608, 5.23529411764706
    ), sd = c(1.1524816893289, NA, 1.31214449357814, 1.5422430010719, 
    1.12039650223724, 1.15104553532809, 1.37714471881058, 1.34621721218454, 
    1.30030385262334, 0.871099231072865, 0.830963499839951, 1.36945187401243
    )), row.names = c(NA, 12L), class = c("tbl_df", "tbl", "data.frame"
))

C <- structure(list(question = c("Q1", "Q10", "Q11", "Q12", "Q2", 
"Q3", "Q4", "Q5", "Q6", "Q7", "Q8", "Q9"), n = c(13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L), mean = c(5.38461538461539, 
4.38461538461539, 4.69230769230769, 4.30769230769231, 5.15384615384615, 
5.38461538461539, 4.76923076923077, 5.30769230769231, 4.53846153846154, 
5.61538461538461, 5.69230769230769, 4.92307692307692), sd = c(1.26085034391223, 
1.44559454541846, 1.03155347127648, 1.60128153805087, 0.898717034272917, 
1.12089707663561, 1.01273936708367, 0.85485041426511, 0.967417922046845, 
1.26085034391223, 0.85485041426511, 1.84668795692624)), row.names = c(NA, 
12L), class = c("tbl_df", "tbl", "data.frame"))

# 绘图代码
ggplot() +
  # 绘制数据集B的柱子
  geom_bar(data = filter(B, question %in% c("Q1", "Q2")),
           aes(x = question, y = mean, fill = "数据集B"),
           stat = "identity", position = position_dodge(width = 0.5), width = 0.45) +
  # 绘制数据集C的柱子
  geom_bar(data = filter(C, question %in% c("Q1", "Q2")),
           aes(x = question, y = mean, fill = "数据集C"),
           stat = "identity", position = position_dodge(width = 0.5), width = 0.45) +
  # 绘制数据集B的误差棒
  geom_errorbar(data = filter(B, question %in% c("Q1", "Q2")),
                aes(x = question, ymin = mean - sd, ymax = mean + sd, color = "数据集B"),
                width = 0.1, position = position_dodge(width = 0.5)) +
  # 绘制数据集C的误差棒
  geom_errorbar(data = filter(C, question %in% c("Q1", "Q2")),
                aes(x = question, ymin = mean - sd, ymax = mean + sd, color = "数据集C"),
                width = 0.1, position = position_dodge(width = 0.5)) +
  # 自定义颜色与图例
  scale_fill_manual(name = "分组", values = c("数据集B" = "#1f77b4", "数据集C" = "#ff7f0e")) +
  scale_color_manual(name = "分组", values = c("数据集B" = "#1f77b4", "数据集C" = "#ff7f0e")) +
  coord_flip() +
  theme_bw()

效果说明

  • 不需要合并两个原始数据框,完全符合不使用bind_rows的需求
  • 误差棒和对应柱子完全对齐,不会出现错位
  • 自动生成统一的分组图例,柱子填充色和误差棒颜色对应

内容的提问来源于stack exchange,提问作者Espejito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 13:33:04