Home > Blockchain >  Legend with geom_line and geom_ribbon and geom_point
Legend with geom_line and geom_ribbon and geom_point

Time:06-05

I've been searching the answer for two days and still can't find how to do this. The closest cases I found How to put points to the legend?

CodePudding user response:

If you want to get a legend you have to map on an aesthetic, e.g. you could map on the shape aes to get a legend for your points too:

library("ggplot2")

ggplot(MyDF, aes(x = Year, y = TheData))  
  geom_point(aes(y = TheData, shape = "TheData"), size = 2.5)  
  geom_line(aes(x = Year, y = Model, color = "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")  
  labs(shape = "")

  • Related