Search code examples
rggplot2scalelegendlegend-properties

How to combine fill (columns) and color (points and lines) legends in a single plot made with ggplot2?


In a chart I have columns and points. I'm trying to unify the legend; I've already put the same name on the scales, but they still appear separate. Could someone please help me solve this issue?

library(ggplot2)

X <- factor(c("a", "b"))
Y1 <- c(10, 15)
Y2 <- c(22, 23)

df <- data.frame(X, Y1, Y2)

ggplot(data = df, aes(x = X,
                      group = 1)) +
  geom_col(aes(y = Y1,
               fill = "Y1")) +
  geom_line(aes(y = Y2,
                color = "Y2")) +
  geom_point(aes(y = Y2,
                 color = "Y2")) +
  scale_fill_manual(name = "Legend",
                    values = "blue") +
  scale_color_manual(name = "Legend",
                     values = "red")

enter image description here


Solution

  • To merge the legends you also have to use the same values in both the fill and color scale. Additionally you have to tweak the legend a bit by removing the fill color for the "Y2" legend key using the override.aes argument of guide_legend:

    EDIT Thanks to the comment by @aosmith. To make this approach work for ggplot2 version <=3.3.3 we have to explicitly set the limits of both scales.

    library(ggplot2)
    
    X <- factor(c("a", "b"))
    Y1 <- c(10, 15)
    Y2 <- c(22, 23)
    
    df <- data.frame(X, Y1, Y2)
    
    ggplot(data = df, aes(x = X,
                          group = 1)) +
      geom_col(aes(y = Y1,
                   fill = "Y1")) +
      geom_line(aes(y = Y2,
                    color = "Y2")) +
      geom_point(aes(y = Y2,
                     color = "Y2")) +
      scale_fill_manual(name = "Legend",
                        values = c(Y1 = "blue", Y2 = "red"), limits = c("Y1", "Y2")) +
      scale_color_manual(name = "Legend",
                         values = c(Y1 = "blue", Y2 = "red"), limits = c("Y1", "Y2")) +
      guides(color = guide_legend(override.aes = list(fill = c("blue", NA))))