[英]ggplot2 two different legends for geom_line
I want to create a time series where i can add 2 legends to the plot.我想创建一个时间序列,我可以在其中向 plot 添加 2 个图例。 both concerning geom_line data.两者都涉及 geom_line 数据。
i can't figure out how to make the second legend without losing the color codng in both legends.我无法弄清楚如何制作第二个图例而不丢失两个图例中的颜色编码。 to create the second legend i used aes(fill) but that doesn't work with geom_line().为了创建第二个图例,我使用了 aes(fill),但这不适用于 geom_line()。
as i have multiple "other_data" columns, just leaving it in the same legend won't do.因为我有多个“other_data”列,所以将它留在同一个图例中是行不通的。 is there a way to genereate the second color legend?有没有办法生成第二个颜色图例? or if not is there a way to rearange the legend?或者如果没有,有没有办法重新排列传说? i have seen a few questions/answers tangential to that topic (like changing the factor level) but couldn't make them work for me.我已经看到了一些与该主题相关的问题/答案(例如更改因子级别),但无法使它们对我有用。
test2<-structure(list(years = 1997:2019, C_GST = c(-4.17580548007064,
-3.09222451295899, -3.81473627184177, -3.86006814181853, -3.44761337632627,
-4.33137213954075, -3.99481467427052, -4.67860991787106, -3.81108136976512,
-2.74141090713935, -3.68934407438251, -4.11076769912514, -4.04671445949792,
-2.15032596522459, -2.45715066933932, -2.44189233075241, -3.55792883020528,
-3.85110566122781, -3.30431922214085, -2.8560678360313, -2.51873859956014,
-4.10634101473384, -3.27696046637259), C_T1m = c(-4.95727614933516,
-4.32259694834445, -4.37292658343919, -4.58082385100315, -4.33745424306488,
-4.86012804392352, -4.72431314874494, -5.10814300904689, -4.77416614034652,
-3.80115724403269, -4.25658945630866, -4.70270307212779, -4.72455672433521,
-3.55976008896879, -3.36917817323156, -3.32223992491526, -4.12645917098195,
-4.62634923771643, -4.21031109860841, -3.71252982450547, -3.55192389326146,
-4.39008989099236, -4.24677580837391), C_T2m = c(-4.97224450466438,
-4.58888176125316, -4.36955390625902, -4.67313492511159, -4.47018178252695,
-4.84373051761506, -4.76878666828251, -5.06758621666582, -4.89644114993229,
-4.08382252158516, -4.29726986542675, -4.72793238335654, -4.76421709959542,
-3.88501639178557, -3.55444156650311, -3.53911074415242, -4.11014209084617,
-4.64427711114094, -4.33303207277665, -3.93464317807564, -3.73412160239998,
-4.29090100987888, -4.38945036072465), other_data = c(NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 5.16122716, -3.53491172, -4.25958996,
-3.18504176, -3.51510776, -3.10949332, -3.58185444, -3.78722884,
-3.39848444, -3.62292932, -3.64126632, -4.69234316, 3.2857188
)), row.names = c(NA, -23L), class = "data.frame")
test2 %>%
ggplot() +
geom_line(aes(x = years, y = C_GST, color = C_GST), size = 1.0, alpha = 0.95, show.legend = T) +
geom_line(aes(x = years, y = C_T1m, color = C_T1m), size = 1.0, alpha = 0.95, show.legend = T) +
geom_line(aes(x = years, y = C_T2m, color = C_T2m), size = 1.0, alpha = 0.95, show.legend = T) +
geom_line(aes(x = years, y = other_data, fill = "Other_Data"), size = 1.1, alpha = 0.95, show.legend = T) +
labs(title = "Placeholder Title",
subtitle = "",
x = "Years",
y = "Temp [°C]",
color = "Depth",
fill = "Depth2")+
theme_calc() +
scale_color_brewer(palette = "Set1")
With the ggnewscale package:使用ggnewscale package:
library(ggplot2)
library(ggnewscale)
ggplot(test2) +
geom_line(aes(x = years, y = C_GST, color = C_GST), size = 1.0, alpha = 0.95, show.legend = T) +
geom_line(aes(x = years, y = C_T1m, color = C_T1m), size = 1.0, alpha = 0.95, show.legend = T) +
geom_line(aes(x = years, y = C_T2m, color = C_T2m), size = 1.0, alpha = 0.95, show.legend = T) +
new_scale_color() +
geom_line(aes(x = years, y = other_data, color = "Other_Data"), size = 1.1, alpha = 0.95, show.legend = T)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.