I've been searching the answer for two days and still can't find how to do this. The closest cases I found here and here. But the former has no points on the plots and the latter has no answer. Without much ado, how to add points to my legend? This is my data:
Year <- c(2003:2020)
TheData <- c(72.6, 72.7, 72.6, 72.5, 72.4, 72.1, 71.8, 71.7, 71.8, 72.3, 72.7,
72.9, 73.1, 73.3, 73.8, 74.7, 75.7, 77.1)
Lower <- c(72.33316, 72.05961, 71.8218, 71.62303, 71.46657, 71.35567, 71.29362,
71.28368, 71.32915, 71.43331, 71.59947, 71.83096, 72.13113, 72.50333,
72.95092, 73.47728, 74.08581, 74.77989)
Upper <- c(73.46626, 73.24078, 73.05676, 72.91817, 72.82899, 72.79323, 72.81489,
72.89794, 73.04639, 73.26418, 73.55528, 73.92363, 74.37315, 74.90775,
75.53132, 76.24776, 77.06094, 77.97473)
Model <- c(72.89971, 72.65020, 72.43928, 72.27060, 72.14778, 72.07445, 72.05425,
72.09081, 72.18777, 72.34874, 72.57738, 72.87730, 73.25214, 73.70554,
74.24112, 74.86252, 75.57337, 76.37731)
MyDF <- data.frame(Year, TheData, Lower, Upper, Model)
This is my code:
library("ggplot2")
ggplot(MyDF, aes(x = Year, y = TheData)) +
geom_point(aes(y = TheData), size = 2.5) +
geom_line(aes(x = Year, y = Model, color = "Model", fill = "Model")) +
geom_ribbon(aes(ymin = Lower, ymax = Upper, x = Year,
fill = "Confidence Interval"), alpha = 0.15) +
scale_colour_manual(
name = "", values = c("Confidence Interval" = "transparent",
"Model" = "black")) +
scale_fill_manual(
name = "", values = c("Confidence Interval" = "grey12",
"Model" = "transparent")) +
theme(legend.position = "bottom")