tidyverse/ggplot2

Version 3.5 Regression Regarding Unused Colour Legend Levels

Closed this issue · 1 comments

This looks fine in version 3.4 but not in version 3.5+.

plotData <- data.frame(x = factor(rep(1:10, 2)),
                       y = factor(rep(1:2, each = 10)),
                       categories = factor(sample(LETTERS[2:4], 20, replace = TRUE), levels = LETTERS[1:5]))

library(ggplot2)
metricColours <- c("#FFFFFF", "#CFD1F2", "#9FA3E5", "#6F75D8", "#3F48CC")
ggplot(plotData, aes(x, y)) + geom_tile(aes(fill = categories)) +
    scale_fill_manual(name = "Test", values = metricColours, drop = FALSE)

Note the grey colours for factor levels not in the data set (A and E) in version 3.5 (looks fine with 3.4.2).

Image
> sessionInfo()
R Under development (unstable) (2025-07-08 r88392 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)
    ...        ...
other attached packages:
[1] ggplot2_3.5.2.9002

Thank you for the report. This is intended and has been discussed before.