Skip to content

drop = FALSE is not working properly (enforcing empty space for some missing levels in a boxplot) #4877

Closed as not planned
@LuisLauM

Description

@LuisLauM

In the following example (using the iris dataset), I am creating a factor class variable in which one of the species does not contain values of level C. When I make the plot, I cannot find a way to make ggplot not drop the empty level (virginica-C). It suppose that the argument drop = FALSE do that, but it does not.

require(dplyr)
require(ggplot2)

iris %>% 
  
  mutate(fct_x = factor(x = sample(x = c("A", "B", "C"), size = nrow(.), replace = TRUE), 
                        levels = c("A", "B", "C"))) %>% 
  
  filter(!(Species == "virginica" & fct_x == "C")) %>% 
  
  ggplot(aes(x = Species, y = Sepal.Length, fill = fct_x)) +
  
  geom_boxplot() +
  
  scale_fill_discrete(drop = FALSE)

In other words, as you can see in the figure that I am attaching, the virginica group does NOT show an empty space for group C (because there are no elements of type virginica-C) and that is exactly what I want to achieve: to show that empty space in the figure.

Rplot

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