Skip to content
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

Manual fill scale guide issue with unused factor level #6201

Closed
jmgirard opened this issue Nov 25, 2024 · 2 comments
Closed

Manual fill scale guide issue with unused factor level #6201

jmgirard opened this issue Nov 25, 2024 · 2 comments

Comments

@jmgirard
Copy link

I found a problem with the guide for a manual fill scale when a factor level is unused.

I expected the guide tile for group C to be green instead of transparent/grey.

Here is the code to reproduce the bug:

library(ggplot2)

data <- data.frame(
  x = c(1, 2, 3, 4),
  ymin = c(1, 1, 2, 2),
  ymax = c(2, 2, 3, 3),
  group = factor(c("A", "B", "A", "B"), levels = c("A", "B", "C"))
)

ggplot(data, aes(x = x, ymin = ymin, ymax = ymax, fill = group)) +
  geom_ribbon() +
  scale_fill_manual(
    values = c("A" = "red", "B" = "blue", "C" = "green"),
    limits = c("A", "B", "C"),
    drop = FALSE
  )

Created on 2024-11-25 with reprex v2.1.1

@teunbrand
Copy link
Collaborator

Hi there, thanks for the report! Since ggplot2 3.5.0 we require data to be present to show the key in the legend or to set geom_*(show.legend = TRUE) if it should be shown regardless. This issue has been reported before in #5728.

@teunbrand teunbrand closed this as not planned Won't fix, can't repro, duplicate, stale Nov 25, 2024
@jmgirard
Copy link
Author

jmgirard commented Nov 25, 2024

library(tidyverse)

data <- data.frame(
  x = c(1, 2, 3, 4),
  ymin = c(1, 1, 2, 2),
  ymax = c(2, 2, 3, 3),
  group = factor(c("A", "B", "A", "B"), levels = c("A", "B", "C"))
)

ggplot(data, aes(x = x, ymin = ymin, ymax = ymax, fill = group)) +
  geom_ribbon(show.legend = TRUE) +
  scale_fill_manual(
    values = c("A" = "red", "B" = "blue", "C" = "green"),
    limits = c("A", "B", "C"),
    drop = FALSE
  )

Created on 2024-11-25 with reprex v2.1.1

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

2 participants