Skip to content

Version 4.0.0 issue with facet_grid & facet_wrap randomly drops levels from fill aes #6680

@kjmtaylor22

Description

@kjmtaylor22

I found a problem with version 4.0.0 that is corrected only by returning to version 3.5.2 and may have something to do with the architecture migration. In short, a geom_area plot (with fill) that does not use faceting will show all fill levels, but once the faceting (both facet_grid and facet_wrap) is added, random levels are dropped from the plot but not from the legend. In this case, the last group facet (with containing just 1 subgroup level) does not fill.

I am using R v4.5.1 (RStudio v2024.12.1.563) on Ubuntu 22.04.

Here is example code that reproduces the bug on my machine in ggplot2 v4.0.0:

df <- data.frame(
x=c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
y=c(74.04, 30.63, 17, 9.42, 3.12, 9.75, 2.54, 1.4, 1.28, 0.25, 0.22, 22.05, 10.5, 9.45, 7.41, 1.69, 24.6, 11, 70, 57.78, 23.91, 17, 7.73, 2.53, 9.75, 2.54, 1.4, 1.28, 0.25, 0.22, 14.7, 10.5, 9.45, 4.94, 1.44, 25.75, 11.55, 70),
group=rep(rep(LETTERS[1:5],times=c(5,6,5,2,1)), 2),
subgroup=rep(letters[1:19],2),
col=rep(c('#6A0215','#9A031E','#FA0F3A','#FB4062','#FC738C','#5A2B0D','#813B0F','#A84B11','#CF5B13','#E87B36','#EC8A4D','#104755','#1E8099','#2CB9DD','#5AC8E4','#8AD8EC','#666666','#CCCCCC','#0B3B0B'),2)
)

## Without faceting
ggplot(df, aes(x, y, fill=subgroup)) +
  scale_fill_manual(breaks=df$subgroup, values=df$col) + 
  geom_area(position="stack", color='white', linewidth=0.1)

## With faceting
ggplot(df, aes(x, y, fill=subgroup)) +
  facet_wrap(vars(group), ncol=1, scales="free_y",  
             strip.position = "right", as.table = T) +
  scale_fill_manual(breaks=df$subgroup, values=df$col) + 
  geom_area(position="stack", color='white', linewidth=0.1)

Using the same code in v3.5.2, both plots run just fine. I will be reverting to v3.5.2 in the meantime.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions