tidyverse/ggplot2

Fill colours are ignored in draw_key_rect

Closed this issue · 2 comments

A regression introduced in ggplot2 v4.0.0 resulting from #6327 results in grey20 fills when a valid fill is mapped.

ggplot2 v.3.5.2

library(ggplot2)

# Column chart
mtcars |> 
  ggplot(aes(x = factor(cyl), fill = factor(cyl))) +
  geom_col(aes(y = mpg), key_glyph = draw_key_rect)

Created on 2025-09-12 with reprex v2.1.1

ggplot2 v4.0.0

library(ggplot2)

# Column chart
mtcars |> 
  ggplot(aes(x = factor(cyl), fill = factor(cyl))) +
  geom_col(aes(y = mpg), key_glyph = draw_key_rect)

Created on 2025-09-12 with reprex v2.1.1

Thanks for the report Mitchell! I'm pretty sure this would have to do with #5385:

ggplot2/NEWS.md

Lines 288 to 289 in 3142fac

* `draw_key_rect()` replaces a `NA` fill by the `colour` aesthetic
(@teunbrand, #5385, #5756).

But I agree with you that this outcome is undesired.

Yes, my investigation found that the fix for #5385 (#6327) introduced the bug - I was also amused by the PR's commit log 😄