Skip to content

scale_fill_brewer() with drop = FALSE no longer shows colors for unused factor levels in the legend  #6091

@dsconnell

Description

@dsconnell

I found a problem with version 3.5.1 of ggplot2 where using scale_fill_brewer() with the option drop = FALSE no longer shows colors for unused factor levels in the legend. You can see in the reprex that all five colors show up in the second plot where all factor levels are used. I expected the legend to look like that even when there are unused factor levels, as in the first plot.

A colleague with ggplot2 version 3.4.3 gets the expected legend. I downgraded to 3.4.3 and also got the the expected legend even when factor levels are unused. I'm not sure in what version the regression occurred, just that it works in 3.4.3 and doesn't work in 3.5.1.

Here is the code to reproduce the bug:

library(ggplot2)

starwars2 <- head(dplyr::starwars, n = 17)
starwars2$species <- factor(starwars2$species)

palette <- "RdYlGn"

ggplot(head(starwars2), aes(x = name)) +
    geom_col(aes(y = height, fill = species)) +
    scale_fill_brewer(palette = palette, drop = FALSE) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))

ggplot(starwars2, aes(x = name)) +
    geom_col(aes(y = height, fill = species)) +
    scale_fill_brewer(palette = palette, drop = FALSE) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))

Created on 2024-09-07 with reprex v2.1.1

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