ggplot - how to group observations into an 'other' category in the legend guide?


I'm having trouble taking an aesthetic mapping like 'color' and associating a different number of labels in the resulting guide.

In my scenario, I have N items in a dataframe but I would only like to highlight 1 or 2 items with a color and leave the remaining items gray. In the resulting guide, I'd like to call out those 1 or 2 items but group the remaining items as "Other".

I'll use mtcars as a toy example. As you can see, the best I can achieve seems to be listing each gray item individually as "Other" which won't work once I have 20+ items.

Instead, here is a doctored version of what I'm envisioning:

R code below

# Regular plot output
cars_head <- mtcars %>% mutate(car_name = rownames(mtcars)) %>% head()
ggplot(cars_head) + geom_point(aes(x=mpg,y=disp,color=car_name))

# Assign which cars to highlight
highlight <- c("Datsun 710"="red","Hornet 4 Drive"="blue", "Hornet Sportabout"="gray",
               "Mazda RX4"="gray","Mazda RX4 Wag"="gray","Valiant"="gray")

# Plot with manual scale. The legend shows each car individually
ggplot(cars_head) + geom_point(aes(x=mpg,y=disp,color=car_name)) + 
  scale_color_manual(values = highlight)

# When using identical labels and colors, the legend items are not grouped
ggplot(cars_head) + geom_point(aes(x=mpg,y=disp,color=car_name)) + 
  scale_color_manual(values = highlight, labels = c("Datsun 710","Hornet 4 Drive","Other","Other","Other","Other"))

This is super-hacky and I'm sure there is a better way, but I think it works?!


mtcars %>%
    car_name = rownames(mtcars),
    color = case_when(
      car_name == "Datsun 710" ~ "red",
      car_name == "Hornet 4 Drive" ~ "blue",
      TRUE ~ "gray"
    color = fct_relevel(color, "gray", after = Inf)
    ) %>% 
  head() %>% 
  ggplot() +
  geom_point(aes(x = mpg, y = disp, color = color)) + 
    labels = c(gray = "Other", red = "Datsun 710", blue = "Hornet 4 Drive"),
    guide = "legend"

Created on 2020-06-08 by the reprex package (v0.3.0)

That doesn't look hacky to me. Seems like that's what the scale_xxxx_identity function is meant for. I wasn't aware of it before, thanks!!

@mfherman shoot, actually this won't work for the time-series data I'm using. It works with geom_point, but with time-series and geom_line, all of the individual 'other' items get lumped together into 1 line.

Leaving this thread as 'solved' because it solves the example with geom_point. I'll have to create a new post with a better example.

Edit: Created new post here: ggplot - how to show individual observations with geom_line but group their color?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.