使用ggplot2时已在aes()中设置color,但图例仍不显示
问题背景
使用ggplot2绘制折线图时图例无法显示,已尝试在aes()内定义color参数,但效果不符合预期。
数据集
date `N Rx AFTN` `N Tx AFTN` SumAFTN `N Rx AMHS` `N Tx AMHS` SumAMHS SUM_Tx SUM_Total SUM_Rx <chr> <int> <int> <dbl> <int> <int> <dbl> <dbl> <dbl> <dbl> 1 2018_M12 22844731 36682853 59527584 5932852 5942479 11875331 42625332 71402915 28777583 2 2019_M12 31858229 52049649 83907878 23412883 25242891 48655774 77292540 132563652 55271112 3 2020_M12 12788348 27191283 39979631 14769682 18274489 33044171 45465772 73023802 27558030 4 2021_M12 11718972 36313167 48032139 22200241 24076705 46276946 60389872 94309085 33919213 5 2022_M12 13362373 46255935 59618308 32268850 31050963 63319813 77306898 122938121 45631223 6 2023_M12 14479914 50957248 65437162 35808014 34036231 69844245 84993479 135281407 50287928
原绘图代码
print(ggplot(all_df))+ geom_point(aes(x=date,y=SUM_Total/1000000))+ geom_line(aes(x=date,y=SUM_Total/1000000,color=SUM_Total),color="blue",group=1)+ geom_line(aes(x=date,y=SUM_Rx/1000000, color=SUM_Rx),color="green",group=1) + geom_line(aes(x=date,y=SUM_Tx/1000000,color=SUM_Tx),color="red",group=1)
数据集dput输出
structure(list(date = c("2018_M12", "2019_M12", "2020_M12", "2021_M12", "2022_M12", "2023_M12"),
N Rx AFTN= c(22844731L, 31858229L, 12788348L, 11718972L, 13362373L, 14479914L),N Tx AFTN= c(36682853L, 52049649L, 27191283L, 36313167L, 46255935L, 50957248L), SumAFTN = c(59527584, 83907878, 39979631, 48032139, 59618308, 65437162),N Rx AMHS= c(5932852L, 23412883L, 14769682L, 22200241L, 32268850L, 35808014L),N Tx AMHS= c(5942479L, 25242891L, 18274489L, 24076705L, 31050963L, 34036231L), SumAMHS = c(11875331, 48655774, 33044171, 46276946, 63319813, 69844245), SUM_Rx = c(28777583, 55271112, 27558030, 33919213, 45631223, 50287928), SUM_Tx = c(42625332, 77292540, 45465772, 60389872, 77306898, 84993479), SUM_Total = c(71402915, 132563652, 73023802, 94309085, 122938121, 135281407)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"), .internal.selfref = <pointer: 0x5613f6742840>, sorted = "date")
问题原因
- 颜色映射逻辑错误:原代码中
aes(color=SUM_Total)是将数值本身作为颜色映射依据,而非按系列(SUM_Total、SUM_Rx、SUM_Tx)区分颜色,不符合图例生成的逻辑。 - 固定颜色覆盖映射:在
geom_line()外部直接指定color="blue"这类固定颜色,会覆盖aes()内的颜色映射,导致ggplot无法识别系列分类,进而不生成图例。
解决方案
方案1:调整原代码,正确设置颜色映射
将系列名称作为分类变量放入aes()的color参数中,移除外部固定颜色,再通过scale_color_manual()指定对应颜色:
ggplot(all_df) + geom_point(aes(x = date, y = SUM_Total/1000000)) + geom_line(aes(x = date, y = SUM_Total/1000000, color = "SUM_Total"), group = 1) + geom_line(aes(x = date, y = SUM_Rx/1000000, color = "SUM_Rx"), group = 1) + geom_line(aes(x = date, y = SUM_Tx/1000000, color = "SUM_Tx"), group = 1) + scale_color_manual(values = c("SUM_Total" = "blue", "SUM_Rx" = "green", "SUM_Tx" = "red")) + labs(y = "数值(百万)", color = "系列")
方案2:转换为长格式数据(ggplot推荐方式)
ggplot更适配长格式数据,先将宽格式数据转换为长格式,再绘图,代码更简洁易维护:
library(tidyr) # 转换为长格式 long_df <- all_df %>% select(date, SUM_Total, SUM_Rx, SUM_Tx) %>% pivot_longer(cols = -date, names_to = "series", values_to = "value") %>% mutate(value = value / 1000000) # 绘图 ggplot(long_df, aes(x = date, y = value, color = series, group = series)) + geom_line() + geom_point(data = long_df %>% filter(series == "SUM_Total")) + # 仅为SUM_Total添加散点 scale_color_manual(values = c("SUM_Total" = "blue", "SUM_Rx" = "green", "SUM_Tx" = "red")) + labs(y = "数值(百万)", color = "系列")
内容的提问来源于stack exchange,提问作者Tassos Pan

