r - ggplot2 geom_line 的两个不同图例
问题描述
我想创建一个时间序列,我可以在其中添加 2 个图例。两者都涉及 geom_line 数据。
我无法弄清楚如何制作第二个图例而不丢失两个图例中的颜色编码。为了创建第二个图例,我使用了 aes(fill),但这不适用于 geom_line()。
因为我有多个“other_data”列,所以将它留在同一个图例中是行不通的。有没有办法生成第二个颜色图例?或者如果没有,有没有办法重新排列传说?我已经看到了一些与该主题相关的问题/答案(例如更改因子级别),但无法使它们对我有用。
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")
解决方案
使用ggnewscale包:
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)
推荐阅读
- entity-framework - 为什么当我尝试使用实体框架将表单保存到测验模型中时出现错误?
- r - 关于圆扇形标签文字大小的变化
- r - R. 我正在尝试将我的数据框按数十年进行子集化。因此,我想通过使用列的值进行子集化
- javascript - 如何检查变量是 Set 还是 Map
- c - 如何使浮点数停止舍入为整数?
- c# - 使用 Autofac ASP.Net Core 5 注册动态通用存储库
- python - 如何从 pip 需求文件创建 AzureML 环境
- c++ - 如何在 Linux 中检查与套接字服务器的现有连接?
- java - JaveFX 中的 GridPane
- java - 如何从字符串中删除子字符串?