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

scale_colour_manual() silently ignores drop = TRUE #4723

Closed
dholstius opened this issue Feb 3, 2022 · 3 comments
Closed

scale_colour_manual() silently ignores drop = TRUE #4723

dholstius opened this issue Feb 3, 2022 · 3 comments

Comments

@dholstius
Copy link

This might be expected behavior but it seems to conflict with the documentation:

Reprex

As Expected

library(tidyverse)

df <- mutate(mtcars, cyl = factor(cyl, levels = as.character(c(2,4,6,8))))

p <- ggplot(df, aes(mpg, wt, color = cyl)) + geom_point()
p + scale_colour_discrete(drop = TRUE)  # omits "2" from legend, as expected
p + scale_colour_discrete(drop = FALSE) # puts "2" in legend, as expected

Unexpected

# imagine this is a palette being used widely across an organization
my_global_palette <- c("2" = "black", "4" = "orange", "6" = "blue", "8" = "darkgreen") 

# puts "2" in legend, even though it's not in the levels that appear in the data
p + scale_colour_manual(values = my_global_palette, drop = TRUE) 

Description

?scale_manual says:

drop
Should unused factor levels be omitted from the scale? The default, TRUE, uses the levels that appear in the data; FALSE uses all the levels in the factor.

scale_colour_manual(..., drop = TRUE) silently accepts the drop argument, but in the last example above, the factor level "2" is still shown in the legend.

Maybe the intent is for values to take precedence over the drop argument, but (a) this is unclear from the documentation, and (b) I think a case could be made against it. A user may wish to define and reuse a default palette without all elements of that palette appearing every time. (Maybe that's achievable in some other way?)

@dholstius
Copy link
Author

Looks like this may be resolved by #4619 — I'll be patient and use limits = force in the meantime.

Will drop = FALSE be honored in the future, even though the default will correspond to drop = TRUE?

@teunbrand
Copy link
Collaborator

As far as I can tell, it does what you expect in the development version of ggplot2.

# devtools::install_github("tidyverse/ggplot2")
library(tidyverse)

my_global_palette <- c("2" = "black", "4" = "orange", "6" = "blue", "8" = "darkgreen") 

df <- mutate(mtcars, cyl = factor(cyl, levels = as.character(c(2,4,6,8))))

p <- ggplot(df, aes(mpg, wt, color = cyl)) + geom_point()

p + scale_colour_manual(values = my_global_palette, drop = TRUE) 

p + scale_colour_manual(values = my_global_palette, drop = FALSE) 

Created on 2022-02-03 by the reprex package (v2.0.1)

@yutannihilation
Copy link
Member

Closing as this is already resolved by #4619. (If I'm missing something, please let me know)

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