ggplot2 geom_line 的两个不同图例

Posted

技术标签:

【中文标题】ggplot2 geom_line 的两个不同图例【英文标题】:ggplot2 two different legends for geom_line 【发布时间】:2021-07-29 15:33:55 【问题描述】:

我想创建一个时间序列,我可以在其中添加 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") 

【问题讨论】:

【参考方案1】:

使用 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) 

【讨论】:

这几乎解决了问题。但我仍然无法为第二个图例添加调色板 @D.J 请参阅 ggnewscale 文档中的示例。您可以添加scale_color_XXX 我做到了,我不得不承认它并没有真正的帮助。我现在通过反复试验几乎管理了所有事情,但其中一个图例拒绝显示正确的线型。

以上是关于ggplot2 geom_line 的两个不同图例的主要内容,如果未能解决你的问题,请参考以下文章

ggplot2中同一图例中的不同图例键

用ggplot2添加图例[重复]

ggplot2:图例混合颜色和隐藏线用于预测图

ggplot2 的 geom_line 中线的大小不同

在 ggplot2 中添加图例

在 ggplot2 中,使用两种图形类型时如何缩放图例?