Skip to content

Mapping alpha within geom_col changes the sorting of the column #6452

Closed as not planned
@margaret-colville

Description

@margaret-colville

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

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