Skip to content

Computed prop is hard to use. #5505

@teunbrand

Description

@teunbrand

This issue was first brought to my attention by @yjunechoe a while back, and thought it was salient enough to merit an issue.
In stat_count() there is a computed variable prop that is basically the count divided by the total count in the group.
Because the default group is the interaction between all discrete variables, this makes vanilla-use of prop a bit pointless:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.3.1
p <- ggplot(diamonds, aes(cut, fill = color)) +
  geom_bar(aes(y = after_stat(prop)), position = "dodge")
p

It only starts making sense if you manually set groups:

p + aes(group = color)

However, you cannot compute the proportion within a position:

p + aes(group = cut)
#> Warning: The following aesthetics were dropped during statistical transformation: fill
#> ℹ This can happen when ggplot fails to infer the correct grouping structure in
#>   the data.
#> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
#>   variable into a factor?

Created on 2023-11-02 with reprex v2.0.2

And there are variants of prop one might be interested in:

  • Group-wise proportion (basically current prop)
  • Position-wise proportion (difficult to compute as user)
  • Panel-wise proportion (difficult to compute as user)
  • Proportion of total (can easily be computed by aes(y = after_stat(count / sum(count))))

So the suggestion here is to add the last 3 variants as prop_position, prop_panel and prop_total (or prop_layer) as new computed variables to stat_count().

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