0

I'm creating a scatterplot and specifying color using a separate geom_point layer. If I don't specify color, GGPLOT will give me a legend.


mtcars_new <- mtcars %>% mutate(cyl_label = if_else(cyl > 6, "High CYL", ""),
                  carb_label = if_else(carb > 6, "High CARB", ""))

p <-  mtcars_new %>%
  ggplot(aes(x = hp, y = mpg)) + 
  geom_point(size = 3, color = "green4") 

p + geom_point(mtcars_new %>% filter(cyl_label == "High CYL"), 
             mapping = aes(color = cyl_label), size = 3)

Image with default color

However, if I supply a specific color, GGPLOT does not give me the legend.

p + geom_point(mtcars_new %>% filter(cyl_label == "High CYL"), 
             mapping = aes(color = cyl_label), size = 3, color = "grey50")

Plot with specifying a specific color

Why does this happen and what can I do to address it?

user438383
  • 5,716
  • 8
  • 28
  • 43
Pss
  • 553
  • 4
  • 12

1 Answers1

1

By setting the color additionally as an argument you are overriding the color aesthetic, i.e. it gets "dropped" and you don't get a legend. Instead I would suggest to simply map on the color aesthetic and use scale_color_manual to set your desired colors.

library(dplyr)
library(ggplot2)

mtcars_new <- mtcars %>% mutate(
  cyl_label = if_else(cyl > 6, "High CYL", ""),
  carb_label = if_else(carb > 6, "High CARB", "")
)

mtcars_new %>%
  ggplot(aes(x = hp, y = mpg)) +
  geom_point(aes(color = cyl_label), size = 3) +
  scale_color_manual(
    values = c("High CYL" = "red", "green4"),
    breaks = "High CYL"
  )

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thanks for this. Just a follow up question: Let's say I want to supply multiple colors. ```r p + geom_point(aes(color = cyl_label), size = 3) + geom_point(aes(color = carb_label), size = 3) + scale_color_manual( values = c("High CYL" = "red", "High CARB" = "purple", "green4"), breaks = c("High CYL", "High CARB")) ``` This doesn't give me the another legend. How do I do that? – Pss Mar 16 '23 at 22:48
  • 1
    In general you get one legend per aesthetic. But there is the `ggnewscale` package which allows to have multiple legends for the same aes. However, even with that you could only have one color per point. Therefore I would suggest to map only one variable on the `color` aesthetic, i..e instead of using two ìf_else` to create two variables I would go for `case_when`, i.e. `cyl_label = case_when(carb > 6 ~ "High CARB", cyl > 6 ~ "High CYL", .default = "")` (Note: I used the new syntax for case_when only available in dplyr <= 1.1.0). – stefan Mar 16 '23 at 23:01