Skip to content

scale_colour_manual() silently ignores drop = TRUE #4723

Closed
@dholstius

Description

@dholstius

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?)

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