Skip to content

scale_colour_manual() does not drop factor levels missing in data #4978

Closed
@aphalo

Description

@aphalo

Argument passed to formal parameter drop is ignored, and unused factor levels are not dropped from the legend as indicated in the documentation. This used to work in the past. 3.3.6, 3.3.5, 3.3.4 are broken, 3.3.3 is good.

Reprex for version 3.3.6 (3.3.5, 3.3.4). drop = TRUE ignored in the manual scale, but obeyed by the default scale.

library(ggplot2)
packageVersion("ggplot2")
#> [1] '3.3.6'

my.df <- data.frame(x = rnorm(50),
                    y = rnorm(50),
                    outcome = factor(sample(letters[1:3], 50, replace = TRUE), 
                                     levels = letters[1:4]))

# level "d" is dropped
ggplot(my.df, aes(x, y, colour = outcome)) +
  geom_point()

# level "d" is not dropped
ggplot(my.df, aes(x, y, colour = outcome)) +
  geom_point() +
  scale_colour_manual(values = c(a = "red", b = "green", c = "blue", d = "orange"),
                      drop = TRUE)

Created on 2022-09-04 with reprex v2.0.2

Reprex for version 3.3.3. drop = TRUE obeyed consistently.

library(ggplot2)
packageVersion("ggplot2")
#> [1] '3.3.3'

my.df <- data.frame(x = rnorm(50),
                    y = rnorm(50),
                    outcome = factor(sample(letters[1:3], 50, replace = TRUE), 
                                     levels = letters[1:4]))

# level "d" is dropped
ggplot(my.df, aes(x, y, colour = outcome)) +
  geom_point()

# level "d" is dropped as expected
ggplot(my.df, aes(x, y, colour = outcome)) +
  geom_point() +
  scale_colour_manual(values = c(a = "red", b = "green", c = "blue", d = "orange"),
                      drop = TRUE)

Created on 2022-09-04 with reprex v2.0.2

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