You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言ggplot2:如何为堆叠条形图添加其他数据集的多条折线

报错原因

你遇到的报错属于ggplot2的全局美学映射继承问题:

  • 你将仅堆叠条形图需要的fill、label参数写在了ggplot()的全局美学声明中,所有后续添加的图层都会默认继承这些映射规则
  • 你要叠加折线的rm、dv、da三个数据集里不存在Competency.Official.Rating字段,因此继承后触发变量未找到的错误
解决步骤
  1. 把fill、label这两个仅条形图需要的映射,从全局移动到geom_bar()和geom_text()的独立映射中,避免后续图层继承无效参数
  2. 建议先将三个折线数据集合并为一个数据框,一次性完成折线和散点的绘制,减少重复代码的同时可以通过颜色区分不同自评等级的折线
完整可运行代码
# 加载依赖包
library(ggplot2)

# 原有数据定义
report_Official <- structure(list(Competency.Official.Rating = structure(c(1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L
), .Label = c("demonstrates the value", "development area", "role model"
), class = "factor"), Competency.Name = structure(c(1L, 1L, 1L, 
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), .Label = c("Co-creating the future", 
"Feedback", "Impact", "One company", "One voice", "Simplification"
), class = "factor"), Freq = c(305L, 114L, 70L, 352L, 72L, 80L, 
333L, 88L, 80L, 293L, 38L, 167L, 313L, 20L, 171L, 358L, 59L, 
85L)), class = "data.frame", row.names = c(NA, -18L))

rm <- structure(list(Competency.Self.Rating = structure(c(3L, 3L, 3L, 
3L, 3L, 3L), .Label = c("demonstrates the value", "development area", 
"role model"), class = "factor"), Competency.Name = structure(1:6, .Label = c("Co-creating the future", 
"Feedback", "Impact", "One company", "One voice", "Simplification"
), class = "factor"), Freq = c(75L, 94L, 113L, 180L, 189L, 116L
)), row.names = c(3L, 6L, 9L, 12L, 15L, 18L), class = "data.frame")

dv <- structure(list(Competency.Self.Rating = structure(c(1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("demonstrates the value", "development area", 
"role model"), class = "factor"), Competency.Name = structure(1:6, .Label = c("Co-creating the future", 
"Feedback", "Impact", "One company", "One voice", "Simplification"
), class = "factor"), Freq = c(309L, 337L, 334L, 286L, 294L, 
338L)), row.names = c(1L, 4L, 7L, 10L, 13L, 16L), class = "data.frame")

da <- structure(list(Competency.Self.Rating = structure(c(2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("demonstrates the value", "development area", 
"role model"), class = "factor"), Competency.Name = structure(1:6, .Label = c("Co-creating the future", 
"Feedback", "Impact", "One company", "One voice", "Simplification"
), class = "factor"), Freq = c(105L, 73L, 54L, 32L, 21L, 48L)), row.names = c(2L, 
5L, 8L, 11L, 14L, 17L), class = "data.frame")

# 合并三个折线数据集
line_data <- rbind(rm, dv, da)

# 绘图代码
ggplot(report_Official, aes(x = Competency.Name, y = Freq)) +
  # 堆叠条形图:fill映射移到当前图层独立声明
  geom_bar(stat = "identity", aes(fill = factor(Competency.Official.Rating, levels = c("role model", "demonstrates the value", "development area")))) +
  # 数值标签:label映射移到当前图层独立声明
  geom_text(size = 5, position = position_stack(vjust = 0.5), aes(label = Freq)) +
  # 新增自评折线
  geom_line(data = line_data, aes(color = Competency.Self.Rating, group = Competency.Self.Rating), linewidth = 1.2) +
  # 新增自评数值点
  geom_point(data = line_data, aes(color = Competency.Self.Rating), size = 3) +
  labs(x = "Competence Name", 
       y = "Number of Employees") +
  theme_bw()  +
  ggtitle("Behavior Official Rating") +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(colour = "grey50"),
        plot.title = element_text(size = rel(1.5),
                                  face = "bold", vjust = 1.5),
        axis.title = element_text(face = "bold"),
        legend.key.size = unit(0.5, "cm"),
        legend.key = element_rect(colour = "gray"),
        axis.title.y = element_text(vjust= 1.8),
        axis.title.x = element_text(vjust= -0.5)) +
  scale_fill_brewer(palette = "PuBu")+
  guides(fill=guide_legend(title="Competency Official Rating"),
         color=guide_legend(title="Competency Self Rating")) +
  theme(panel.grid.major = element_blank(), text = element_text(size=15), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  theme(axis.text.x = element_text(face = "bold",
                                   size = 15, angle = 45, hjust = 1),
        axis.text.y = element_text(face = "bold",
                                   size = 15, angle = 90))
可选调整方案

如果不需要自动区分折线颜色,也可以不合并数据集,单独为三个数据集添加图层,替换上述代码中的折线、散点部分即可:

geom_line(data = rm, aes(group = 1), color = "red", linewidth = 1.2) +
geom_point(data = rm, color = "red", size = 3) +
geom_line(data = dv, aes(group = 1), color = "green", linewidth = 1.2) +
geom_point(data = dv, color = "green", size = 3) +
geom_line(data = da, aes(group = 1), color = "blue", linewidth = 1.2) +
geom_point(data = da, color = "blue", size = 3)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 20:18:00