ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Chain stat

Open teunbrand opened this issue 9 months ago • 2 comments

This PR aims to fix #6325.

Briefly, it introduces a new stat stat_chain() that can combine other stats. It uses link_stat() as a helper to parameterise the individual stats that it combines.

I like the 'links in a chain' analogy for this stat better than a 'stack of stats', because while they both indicate a serial nature, for chains it matters how they are interlinked, whereas a stack isn't connected properly. The only downside is that 'link' these days evoke HTML hyperlinks more than chain links, which is a potential source of confusion.

The example section rendered:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

p <- ggplot(mpg, aes(displ, colour = drv))
# Binning unique observations
p + stat_chain(stats = c("unique", "bin"))
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# Controlling parameters
p + stat_chain(
  stats = list("unique", link_stat("bin", bins = 10))
)

# Evaluate expressions after computing stats
p + stat_chain(
  stats = list("unique", link_stat("bin", mapping = aes(y = density)))
)
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

Created on 2025-02-07 with reprex v2.1.1

I've tried to put some thought into the API and I can't really come up with a better solution than the link_stat() helper function. I'd happily consider other suggestions.

teunbrand avatar Feb 07 '25 12:02 teunbrand