ggplot2双轴图图例格式设置:合并图例并删除冗余项
解决ggplot双轴图的图例问题
问题需求
- 将两个独立图例合并为一个统一图例
- 删除图例中多余的
Overall Balance (N$ Million)条目(仅保留柱状图对应类别和Overall Balance (% of GDP, RHS))
修改后的完整代码
dat <- structure(list(Category = c("Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)"), Year = c("2018/19", "2018/19", "2018/19", "2019/20", "2019/20", "2019/20", "2020/21", "2020/21", "2020/21", "2021/22 (est)", "2021/22 (est)", "2021/22 (est)", "2022/23 (proj)", "2022/23 (proj)", "2022/23 (proj)", "2023/24 (proj)", "2023/24 (proj)", "2023/24 (proj)", "2024/25 (proj)", "2024/25 (proj)", "2024/25 (proj)"), Value = c(-9226, -5.09893041491584, -3, -8918, -4.97862890962981, -3, -14197, -8.04324429255841, -3, -16138, -8.8, -3, -12658, -6.5, -3, -10418, -5, -3, -10281, -4.7, -3)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -21L)) library(tidyverse) ggplot() + # 柱状图仅保留fill映射,移除多余的color映射 geom_col(data = subset(dat, Category == "Overall Balance (N$ Million)"), mapping = aes(x = Year, y = Value, fill = Category), position = position_dodge()) + # 折线图保留color映射 geom_line(data = subset(dat, Category == "Overall Balance (% of GDP, RHS)"), mapping = aes(x = Year, y = (Value*1500), color = Category), size = 2) + # 配置fill图例:指定颜色、保留需要的类别,自定义图例样式实现合并 scale_fill_manual( values = c("Overall Balance (N$ Million)" = '#7c3042'), limits = c("Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)"), guide = guide_legend(override.aes = list( fill = c('#7c3042', NA), color = c(NA, '#c7af76'), shape = c(22, NA), linetype = c(0, 1) )) ) + # 配置color映射:指定颜色、匹配limits,关闭单独图例 scale_color_manual( values = c("Overall Balance (% of GDP, RHS)" = '#c7af76'), limits = c("Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)"), guide = "none" ) + theme_light() + labs(x = '') + scale_y_continuous( breaks = seq(-22000,3000,by = 5000), name = 'Overall Balance (N$ Million)', sec.axis= sec_axis(~./1500, name="Overall Balance (% of GDP)", breaks = seq(-10,0, by = 2)) ) + theme(legend.title = element_blank())
关键改动说明
- 移除柱状图的color映射:原代码中柱状图同时设置
fill和color,导致生成两个独立图例,移除后仅通过fill区分柱状图,减少图例来源。 - 合并图例配置:在
scale_fill_manual中通过limits指定需要保留的两个类别,再用guide_legend(override.aes)自定义每个图例条目的显示样式:柱状图对应填充色块、无线条,折线图对应线条、无填充,实现两个类别合并到同一图例。 - 关闭color的单独图例:在
scale_color_manual中设置guide = "none",避免生成第二个独立图例,同时用键值对形式指定颜色与类别的对应关系,确保样式准确。
内容的提问来源于stack exchange,提问作者Tanga94
相关产品推荐
相关产品推荐

