I am trying to add a legend to my graphs in R using ggplot2 but have so far been unsuccessful. I have tried multiple different suggestions that I have seen online but none of them seem to produce a legend.
This is the code that I am using for my graphs (without trying to add a legend):
prrgapuk <- ggplot() +
geom_line(aes(y=uk1, x= Date), color="black", data = rrgap) +
geom_line(aes(y=uk2, x= Date), color = "blue", linetype = "dotdash", data = rrgap) +
geom_line(aes(y=uk3, x= Date), color = "red", linetype = "dashed", data = rrgap) +
geom_line(aes(y=uk4, x= Date), color = "green", linetype="twodash", data = rrgap) +
geom_line(aes(y=uk5, x= Date), color = "purple", linetype="longdash",data = rrgap) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
prrgapuk + geom_hline(yintercept = 0, linetype="dotted") + labs(y="") + geom_rect(data = usrecessions.df, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=Inf), fill="grey", alpha=0.35)
I would like the legend to look something like this
I would be really grateful if someone could point me in the right direction