1

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")

This is my plot. How to put points to the legend?

Pavlo
  • 63
  • 6

2 Answers2

1

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 = "")

stefan
  • 90,330
  • 6
  • 25
  • 51
0

If somebody is interested to move the legend to free space on the plot there is an obvious way to do so:

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 = "") +
theme(legend.position = c(.4, .7))

But the legend appears stacked: enter image description here Adding + guides(color = guide_legend(nrow = 1)) does not work: enter image description here

My colleague have proposed to add legend.box = "horizontal". This code works:

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 = "") +
theme(legend.position = c(.4, .7), legend.box = "horizontal") +
guides(color = guide_legend(nrow = 1))

The plot looks like this: enter image description here

Still, I wonder why the legend appears in different boxes and how to put it together?

Pavlo
  • 63
  • 6