Skip to content

Symbol not shown for manually added values in the legend #6318

Closed
@julou

Description

@julou

Hello,

This might be related to #3372.
I need to combine several plots in a grid and make one shared legend. For this, I used to set manually the legend values and limits in the colour scale. But it doesn't work anymore

Here is a reprex:

ggplot(mpg, aes(class, hwy, col=class)) + 
  geom_boxplot() +
  scale_color_manual(values=c("suv"="blue", "compact"="red", "pickup"="green", "foo"="orange", "bar"="pink"), 
                     limits=c("suv", "compact", "pickup", "foo", "bar") )

Image

The levels are added to the legend ("foo" and "bar") but the corresponding symbols are not shown.

I tried (man many other things) adding a blank geom but it didn't help (same output).

ggplot(mpg, aes(class, hwy, col=class)) + 
  geom_boxplot() +
  geom_blank(data=~mutate(., class=fct_recode(class, "foo"="compact", "bar"="pickup"))) +
  scale_color_manual(values=c("suv"="blue", "compact"="red", "pickup"="green", "foo"="orange", "bar"="pink"), 
                     limits=c("suv", "compact", "pickup", "foo", "bar") )

This used to be with previous version of ggplot2 (3.4.1 on top of my head), but does not anymore with 3.5.1.
Thank you in advance for your guidance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions