Ggplot2:在一个图中使用 scale_fill_manual() 为 geom_rect() 和 geom_line() 创建图例?

Ggplot2: Create legend using scale_fill_manual() for geom_rect() and geom_line() in one plot?

提问人:george1994 提问时间:5/27/2022 最后编辑:Quintengeorge1994 更新时间:5/27/2022 访问量:660

问:

我想创建一个带有 geom_line() 和 geom_rect() 的绘图。对于两者,我都想创造一个传奇,但我不工作。两者如何结合?正如您在所附图片中看到的,我的图例被搞砸了:颜色和形状没有正确显示。

graph= data.frame(seq(as.Date("2004/01/01"), as.Date("2022/01/01"), by="month"),rnorm(217,mean=1,sd=2),rnorm(217,mean=1,sd=2),rnorm(217,mean=1,sd=2))
colnames(graph) = c("Datum","VPI","VPIF","VPIE")

plot1 = ggplot(graph, aes(x = graph$Datum)) +
  geom_line(aes(y = graph$VPI, colour = "black"), size = 0.8) +
  geom_line(aes(y = graph$VPIF, colour = "red"), size = 0.8) +
  geom_line(aes(y = graph$VPIE, colour = "snow4"), size = 0.8) +
  geom_rect(aes(xmin = graph$Datum[49], xmax = graph$Datum[72], ymin = 125, ymax = 130,fill="cyan") ,alpha = 0.5)+
  geom_rect(aes(xmin = graph$Datum[84], xmax = graph$Datum[120], ymin = 125, ymax = 130,fill="darkolivegreen"), alpha = 0.5)+
  geom_rect(aes(xmin = graph$Datum[195], xmax = graph$Datum[217], ymin = 125, ymax = 130,fill="blueviolet"), alpha = 0.5)+
  scale_fill_manual(name=NULL,values=c("black","red","snow4","cyan","darkolivegreen","blueviolet"), labels=c("VPI","VPI Lebensmittel","VPI Energie","Weltfinanzkrise","Euro-/Schuldenkrise","Coronakrise"),aesthetics = c("colour","fill")) +
  theme_bw() +
  theme(legend.position = "bottom",
        axis.text.x     = element_text(angle = 90)) +
  labs(title = "Verbraucherpreisindex: Gesamt, Lebensmittel, Energie", subtitle = "2015=100",
       y     = "Prozent",
       x     = "Jahre")

plot1

enter image description here

r ggplot2 剧情 图例

评论


答:

1赞 Roman 5/27/2022 #1

将数据从宽转换为长。在这里,我使用了 tidyverse 包。但您也可以使用 或 .pivot_longermeltreshape

library(tidyverse)
data_rect <- tibble(xmin = graph$Datum[c(49,84,195)],
                    xmax = graph$Datum[c(72,120,217)],
                    ymin = 50,
                    ymax=53,
                    gr = c("1", "2", "3"))
graph %>% 
  pivot_longer(-1) %>% 
  ggplot(aes(Datum, value)) + 
   geom_line(aes(color = name))  + 
   geom_rect(data=data_rect, inherit.aes = F, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, fill=gr))

enter image description here

1赞 TarJae 5/27/2022 #2

像这样的东西?

graph %>% 
  pivot_longer(
    -Datum
  ) %>% 
  ggplot(aes(x=Datum, y = value)) +
  geom_line(aes(color = name)) +
  scale_color_manual(name = NULL, values = c("black", "red", "snow4"))+
  geom_rect(aes(xmin = graph$Datum[49], xmax = graph$Datum[72], ymin = 125, ymax = 130,fill="cyan") ,alpha = 0.5)+
  geom_rect(aes(xmin = graph$Datum[84], xmax = graph$Datum[120], ymin = 125, ymax = 130,fill="darkolivegreen"), alpha = 0.5)+
  geom_rect(aes(xmin = graph$Datum[195], xmax = graph$Datum[217], ymin = 125, ymax = 130,fill="blueviolet"), alpha = 0.5)+
  scale_fill_manual(name = NULL, values = c("cyan", "darkolivegreen", "blueviolet"))
  theme_bw() +
  theme(legend.position = "bottom",
        axis.text.x     = element_text(angle = 90)) +
  labs(title = "Verbraucherpreisindex: Gesamt, Lebensmittel, Energie", subtitle = "2015=100",
       y     = "Prozent",
       x     = "Jahre")
  

enter image description here