I'd like to add a legend to a ggplot graph and I have not found a similar answer online.
My legend needs to indicate the vertical lines are dates that major events happened and what the other colours represent (eg. smoothed line is a moving average). The vertical lines do not form part of the dataset.
RePrEx:
library(data.table)
library(ggplot2)
library(lubridate)
dat <- as.data.table(x=1:10)
dat$V1 <- as.Date(dat$V1,origin = "2000-01-01")
dat$y_a = c(2,3,4,2,4,2,5,5,4,3)
dat$y_b = c(4,5,5,6,4,3,4,5,6,5)
sections <- as.Date(c(3,5,8),origin = "2000-01-01")
ggplot(data=dat)+
geom_line(aes(x=V1,y=y_a),colour="black")+
geom_smooth(aes(x=V1,y=y_a),alpha=0,colour="blue")+
geom_vline(xintercept = sections,linetype="dashed",colour="red")+
geom_vline(xintercept = as.Date(7,origin = "2000-01-01"),colour="darkgreen")+
xlab("Time")+
ylab("Height")
My example graph is in the link.
It is not possible to explain these graph details elsewhere.