Closed as not planned
Description
I found a problem with creating a column chart where I was trying to highlight some specific segments using transparency. Applying an alpha
aes within the geom_col
changes the order of the sorting for the fill - while doing so within the ggplot
call leaves the ordering untouched.
I expected ordering to be unaffected by transparency in either case.
Here is the code to reproduce the bug:
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
penguins %>%
filter(!is.na(sex)) %>%
group_by(species, island, sex) %>%
summarise(count = n()) %>%
ungroup() %>%
mutate(flag = ifelse(species == 'Chinstrap' & sex == 'male', 'flag', 'no flag')) %>%
ggplot(aes(x = species, y = count, fill = sex)) +
geom_col(aes(alpha = flag)) +
facet_wrap(~island)
#> `summarise()` has grouped output by 'species', 'island'. You can override using
#> the `.groups` argument.
#> Warning: Using alpha for a discrete variable is not advised.
penguins %>%
filter(!is.na(sex)) %>%
group_by(species, island, sex) %>%
summarise(count = n()) %>%
ungroup() %>%
mutate(flag = ifelse(species == 'Chinstrap' & sex == 'male', 'flag', 'no flag')) %>%
ggplot(aes(x = species, y = count, fill = sex, alpha = flag)) +
geom_col() +
facet_wrap(~island)
#> `summarise()` has grouped output by 'species', 'island'. You can override using
#> the `.groups` argument.
#> Warning: Using alpha for a discrete variable is not advised.
Created on 2025-05-13 with reprex v2.1.1
Metadata
Metadata
Assignees
Labels
No labels