Jim
Jim

Reputation: 725

ggplot separate legend for regression lines

I'd like to have two legends on a ggplot scatter plot: one for the point colors, and another for the regression line colors.

Can you explain how to add a separate legend for the two regression lines here?

ggplot(mtcars, aes(x = mpg, y = hp)) +
  geom_point(aes(color = gear)) + 
  geom_smooth(method='lm', formula=y~x, se = FALSE, size = 0.5, color = 'dark green') + 
  geom_smooth(method='lm', aes(mpg, hp), data = mtcars[mtcars$wt > 3, ], se = FALSE, size = 0.5, color = 'dark orange', fullrange = TRUE)

Upvotes: 2

Views: 4651

Answers (2)

Z.Lin
Z.Lin

Reputation: 29095

Two points to note:

  1. By default, ggplot will produce only one legend for each mapped aesthetic, based on all geoms that specify aes(color = XXX, ...). One workaround for creating separate legends is to utilise the fact that some shapes (which can be specified within geom_point()) can take on a fill aesthetic as well as a color aesthetic.

screenshot

Image taken from here. Shapes 21-25 accept both color (indicated in black) & fill (indicated in red).

  1. In order to create a legend, the aesthetic must be specified inside aes(). But sometimes we wish to specify the color directly for each line. One workaround is to use the scale_XXX_manual() option, which allows you to specify aes(color = "some label") within the geom, and map the color in scale_color_manual(values = c("some label" = "some color"))

Code:

ggplot(mtcars,
       aes(x = mpg, y = hp)) +
  geom_point(aes(fill = gear), shape = 21) +          # specify shape here
  geom_smooth(method = 'lm', formula = y~x, 
              se = FALSE, size = 0.5,
              aes(color = "lm1")) +                   # specify color within aes()
  geom_smooth(method = 'lm', se = FALSE, size = 0.5,
              data = mtcars[mtcars$wt > 3, ],
              aes(x = mpg, y = hp, color = "lm2"),    # as above
              fullrange = TRUE) +
  scale_fill_continuous(name = "Points") +            # legend name
  scale_color_manual(name = "Regression",             # legend name
                     values = c("lm1" = "darkgreen",  # map regression line colors
                                "lm2" = "darkorange"))

plot

Upvotes: 5

xiaojia zhang
xiaojia zhang

Reputation: 1058

the issue is because the gear is a numerical value, if you want a separate legend, you need using the discrete value, like factor or character data type, so you can change the layer of geom_point like that:

    geom_point(aes(color = factor(gear))) 

Upvotes: 0

Related Questions