Skip to content

legend key does not respect scale_fill_manual(..., drop = FALSE) #5586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davidhodge931 opened this issue Dec 15, 2023 · 2 comments
Closed

Comments

@davidhodge931
Copy link

davidhodge931 commented Dec 15, 2023

In the below example, the legend key for female should be red with a black border.

I'm using the latest DEV version of ggplot2. It works correctly in 3.4.4.

library(tidyverse)
library(palmerpenguins)

penguins |>
  mutate(sex = factor(sex)) |>
  filter(sex == "male") |>
  ggplot() +
  geom_violin(aes(
    x = sex,
    y = body_mass_g,
    fill = sex,
  )) +
  scale_fill_manual(values = c("red", "blue"), drop = FALSE)

Created on 2023-12-15 with reprex v2.0.2

@thomasp85
Copy link
Member

This is due to a new feature where layers only contribute legends to the data that is shown in the layer. You can reinstate the old behaviour by setting show.legend = TRUE in the layer

@teunbrand
Copy link
Collaborator

Yeah this is intended behaviour from #5502, which you can indeed revert by using show.legend as Thomas says.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants