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

Time:05-27

I want to create a plot with geom_line() and geom_rect(). For both I would like to create a legend, but I does not work. How can both be combined? As you can see in the attached picture, my legend is messed up: Colours and shapes are not correctly displayed.

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

CodePudding user response:

Transform the data from wide to long. Here I used pivot_longer from the tidyverse package. But you can also use melt or reshape.

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

CodePudding user response:

Something like this?

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

  • Related