Facet wrap 使用facet_wrap向ggplot添加图例

Facet wrap 使用facet_wrap向ggplot添加图例,facet-wrap,r,ggplot2,label,data-visualization,Facet Wrap,R,Ggplot2,Label,Data Visualization,我使用来显示四个城市的两个变量(a和b)。我可以根据城市对地块进行分组,但不能使用scale\u color\u离散显示变量(即a和b)的图例。 ggplot() + geom_line(data=df, aes(x=year, y = a, group=city), colour="red",linetype = "longdash",show_guide = TRUE) + geom_line(data=df, aes(x=year, y = b, group=city),

我使用来显示四个城市的两个变量(
a
b
)。我可以根据城市对地块进行分组,但不能使用
scale\u color\u离散显示变量(即
a
b
)的图例。

ggplot() + 
   geom_line(data=df, aes(x=year, y = a, group=city), colour="red",linetype = "longdash",show_guide = TRUE) + 
   geom_line(data=df, aes(x=year, y = b, group=city), colour="blue", show_guide = TRUE) +
   scale_color_discrete(name="Scenarios",labels=c("a" ,"b")) +
   guides(color=guide_legend(ncol=2)) +
   theme(legend.position="bottom")  +
   facet_wrap( ~ city, ncol=2) 

以下是我的数据子集:

structure(list(year = c(2015, 2016, 2016, 2016, 2016, 2016, 2016, 
2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2018, 
2018, 2018), 
          city = c("Calgary", "Calgary", "Calgary", "Halifax", 
"Halifax", "Ottawa", "Ottawa", "Yellowknife", "Yellowknife", 
"Calgary", "Calgary", "Halifax", "Halifax", "Ottawa", "Ottawa", 
"Yellowknife", "Yellowknife", "Calgary", "Calgary", "Halifax"), 
          a = c(25988.04, 37842.33, 37842.33, 11595.46, 11595.46, 49458.24, 
49458.24, 185.31, 185.31, 36718.9, 36718.9, 11176.82, 11176.82, 
47606.45, 47606.45, 176.5, 176.5, 36303.91, 36303.91, 10976.56),        
          b = c(25988.04, 37842.33, 37842.33, 11595.46, 11595.46, 49458.24, 
49458.24, 185.31, 185.31, 36718.9, 36718.9, 11176.82, 11176.82, 
47606.45, 47606.45, 176.5, 176.5, 36303.91, 36303.91, 10976.56
)), row.names = c(NA, -20L), 
          class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = c("year", "city"), drop = TRUE, indices = list(
    0L, 1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18, 
    19L), 
          group_sizes = c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L), 
          biggest_group_size = 2L, labels = structure(list(year = c(2015, 
2016, 2016, 2016, 2016, 2017, 2017, 2017, 2017, 2018, 2018), 
           city = c("Calgary", "Calgary", "Halifax", "Ottawa", "Yellowknife", 
    "Calgary", "Halifax", "Ottawa", "Yellowknife", "Calgary", 
    "Halifax")), row.names = c(NA, -11L), class = "data.frame", vars = c("year", 
"city"), drop = TRUE))

您可以为变量指定颜色,然后使用
缩放\u颜色\u手册
执行此操作,如下所示:

vars <- c("a"="red", "b"="blue")
ggplot() + 
  geom_line(data=df, aes(x=year, y = a, colour="a"), linetype = "longdash") + 
  geom_line(data=df, aes(x=year, y = b, colour="b")) +
  scale_colour_manual(name="Scenarios:", values=vars) +
  theme(legend.position="bottom")  +
  facet_wrap( ~ city, ncol=2) 

vars您可以为变量指定颜色,然后使用
scale\u color\u manual
执行此操作,如下所示:

vars <- c("a"="red", "b"="blue")
ggplot() + 
  geom_line(data=df, aes(x=year, y = a, colour="a"), linetype = "longdash") + 
  geom_line(data=df, aes(x=year, y = b, colour="b")) +
  scale_colour_manual(name="Scenarios:", values=vars) +
  theme(legend.position="bottom")  +
  facet_wrap( ~ city, ncol=2) 

vars正确的方法是将数据重塑为长格式,这样您就有一个关键变量,即
a
b
,以及一个具有相应值的值变量。这使您可以将值映射到
y
和颜色键(以及线型,如果您愿意),单个
geom\u line
调用将自动生成图例。例如<代码>库(tidyverse);df%%>%gather(key,value,a:b)%%>%ggplot(aes(year,value,color=key,linetype=key))+geom_line()+facet_wrap(~city,scales='free_y')
(尽管这些行与给定的数据相互重叠)正确的方法是将数据重新格式化为长格式,这样您就有了一个键变量
a
b
,以及具有相应值的值变量。这使您可以将值映射到
y
和颜色键(以及线型,如果您愿意),单个
geom\u line
调用将自动生成图例。例如<代码>库(tidyverse);df%>%gather(key,value,a:b)%>%ggplot(aes(year,value,color=key,linetype=key))+geom_line()+facet_wrap(~city,scales='free_y')
(尽管这些线与给定数据相互覆盖)