如何在R中手动生成包含颜色、形状和线型的统一图例?
解决ggplot地图图例合并问题
你的代码当前生成了两个独立图例,要合并成包含所有指定元素的统一图例,核心是让所有美学映射共享同一套标签体系,再通过override.aes精准控制每个图例项的显示样式,同时关闭多余的图例引导。
修改后的完整代码
ggplot() + # 底图元素(无需图例,不加入aes映射) geom_sf(data = all_boundaries$osm_multipolygons, color = "lightgrey", size = 0.7, alpha = 0.8) + geom_sf(data = all_boundaries$osm_lines, linetype = "dotted", color = "#D55E00", size = 0.7, alpha = 0.8) + geom_sf(data = coast$osm_lines, color = "#56B4E9", size = 12) + coord_sf(xlim = c(-113.517109, -117.823506), ylim = c(30.742140, 33.961826), expand = FALSE) + theme_bw() + labs(x = NULL, y = NULL) + # 需展示图例的元素:用统一标签关联美学属性 geom_path(data = ros, aes(x = long, y = lat, linetype = "Mexican ducts", color = "Mexican ducts"), alpha = 0.5) + geom_point(aes(x = -116.8476211, y = 31.9885162, color = "LNG terminal", shape = "LNG terminal"), fill = "#CC79A7", size = 2) + geom_line(data = amp, aes(x = long, y = lat), alpha = 0.5) + # 该元素无需图例,不设置标签 geom_path(data = nbaja, aes(x = lat, y = long, linetype = "US ducts", color = "US ducts"), alpha = 0.5) + geom_path(data = sand, aes(x = long, y = lat), linetype = "longdash", alpha = 0.5) + # 无需图例 geom_path(data = soca, aes(x = long, y = lat), linetype = "longdash", alpha = 0.5) + # 无需图例 geom_point(aes(x = -115.428521, y = 32.669708, shape = "Interconnectors", color = "Interconnectors"), fill = "#000000") + geom_point(aes(x = -116.898, y = 32.5534, shape = "Interconnectors", color = "Interconnectors"), fill = "#000000") + geom_point(aes(x = -114.773898, y = 32.715056, shape = "Interconnectors", color = "Interconnectors"), fill = "#000000") + geom_point(data = plants, aes(x = long, y = lat, color = "Power plants", shape = "Power plants"), fill = "#D55E00", alpha = 0.5, size = 1.5) + # 颜色比例尺:统一所有标签的颜色值 scale_color_manual( values = c( "Power plants" = "#D55E00", "LNG terminal" = "#CC79A7", "Interconnectors" = "#000000", "Mexican ducts" = "#999999", "US ducts" = "#999999" ), labels = c("Power plants", "LNG terminal", "Interconnectors", "Mexican ducts", "US ducts") ) + # 形状比例尺:给无需形状的项设为NA scale_shape_manual( values = c( "Power plants" = 21, "LNG terminal" = 15, "Interconnectors" = 23, "Mexican ducts" = NA, "US ducts" = NA ), labels = c("Power plants", "LNG terminal", "Interconnectors", "Mexican ducts", "US ducts") ) + # 线型比例尺:给无需线型的项设为NA scale_linetype_manual( values = c( "Power plants" = NA, "LNG terminal" = NA, "Interconnectors" = NA, "Mexican ducts" = "solid", "US ducts" = "longdash" ), labels = c("Power plants", "LNG terminal", "Interconnectors", "Mexican ducts", "US ducts") ) + # 合并图例:仅保留一个引导,统一设置样式 guides( color = guide_legend( title = "Legend", override.aes = list( shape = c(21, 15, 23, NA, NA), linetype = c(NA, NA, NA, "solid", "longdash") ) ), shape = "none", # 关闭单独的形状图例 linetype = "none" # 关闭单独的线型图例 )
关键修改说明
- 统一标签体系:所有需入图例的元素,用相同标签关联
color/shape/linetype,让ggplot识别它们属于同一组图例项。 - 补充线型比例尺:新增
scale_linetype_manual,给点类元素的线型设为NA,避免干扰显示。 - 合并图例引导:仅保留
color的图例引导,通过override.aes为每个图例项指定对应形状、线型,同时关闭shape和linetype的单独图例。 - 清理冗余设置:移除底图元素的
inherit.aes = TRUE(无需继承映射),以及原scale中多余的guide参数,避免冲突。
内容的提问来源于stack exchange,提问作者Lilia
相关产品推荐
相关产品推荐

