简体   繁体   English

R ggplot2将图例添加到线型

[英]R ggplot2 add legend to linetype

Based on the following code how can I add a legend to the black line ( Historic Trend ) type using ggplotly (Because ggplotly adds numbers (like ,0 ) to legend sometimes which you don't get with a simple ggplot legend)?基于以下代码,如何使用ggplotly将图例添加到黑线( Historic Trend )类型(因为ggplotly有时会向图例添加数字(如,0 ),而简单的ggplot图例无法获得)?

Should I add aes(color = "black") to geom_vline or geom_hline ?我应该将aes(color = "black")添加到geom_vline还是geom_hline

Sample data ( AvgTMeanYear ):样本数据( AvgTMeanYear ):

structure(list(year = 1980:2021, AvgTMean = c(24.2700686838937, 
23.8852956598276, 25.094446596092, 24.1561175050287, 24.157183605977, 
24.3047482638362, 24.7899738481466, 24.5756232655603, 24.5833086228592, 
24.7344695534483, 25.3094451071121, 25.2100615173707, 24.3651692293534, 
24.5423890611494, 25.2492166633908, 24.7005097837931, 24.2491591827443, 
25.0912281781322, 25.0779264303305, 24.403294248319, 24.4983991453592, 
24.4292324356466, 24.8179824927011, 24.7243948463075, 24.5086534543966, 
24.2818632071983, 24.4567195220259, 24.8402224356034, 24.6574465515086, 
24.5440715673563, 23.482670620977, 24.9979594684914, 24.5452453980747, 
24.9271462811494, 24.7443215819253, 25.8929839790805, 25.1801908261063, 
25.2079308058908, 25.0722425561207, 25.4554644289799, 25.4548979078736, 
25.0756772250287)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-42L)) 

Code:代码:

ggT = ggplot(AvgTMeanYear, aes(year, AvgTMean)) + 
  geom_smooth(method = 'lm', fullrange = TRUE,  aes(color = "blue")) +
  annotate('rect', xmin = -Inf, xmax = 2021, ymin = -Inf, ymax = Inf,
           fill = 'gray92') +
  geom_vline(xintercept = seq(1980, 2020, 5), color = 'white') +
  geom_hline(yintercept = seq(23.5, 25.5, 0.5), color = 'white') +
  geom_line() +
  scale_x_continuous(limits = c(1980, 2030)) +
  scale_colour_manual(name = "Legend", values = "blue", label = "Future Trend")

ggplotly(ggT)

If you want to have a legend you have to map on aesthetics, ie use eg geom_line(aes(color = "Historic Trend")) to get a legend entry for the black line too.如果你想要一个图例,你必须在美学上进行映射,即使用例如geom_line(aes(color = "Historic Trend"))来获取黑线的图例条目。 Additionally I slightly adjusted your code an used color ="Future Trend" for your blue line too.此外,我也为您的蓝线稍微调整了您的代码使用的color ="Future Trend" The reason is that using the labels argument of scale_colour_manual did not work when converting via ggplotly() .原因是通过ggplotly()转换时,使用scale_colour_manuallabels参数不起作用。

library(plotly)

ggT <- ggplot(AvgTMeanYear, aes(year, AvgTMean)) + 
  geom_smooth(method = 'lm', fullrange = TRUE,  aes(color =  "Future Trend")) +
  annotate('rect', xmin = -Inf, xmax = 2021, ymin = -Inf, ymax = Inf,
           fill = 'gray92') +
  geom_vline(xintercept = seq(1980, 2020, 5), color = 'white') +
  geom_hline(yintercept = seq(23.5, 25.5, 0.5), color = 'white') +
  geom_line(aes(color = "Historic Trend")) +
  scale_x_continuous(limits = c(1980, 2030)) +
  scale_colour_manual(name = "Legend", values = c( "Future Trend" = "blue", "Historic Trend" = "black"))

ggplotly(ggT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM