Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain stat #6326

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Chain stat #6326

wants to merge 8 commits into from

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Feb 7, 2025

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
Copy link
Collaborator Author

teunbrand commented Feb 25, 2025

Consider:

  • Pass dots to first stat?
  • Better name for linked_stat(mapping), e.g. after_stat?

@teunbrand
Copy link
Collaborator Author

Pass dots to first stat?

This will be more complicated than anticipated.
If we try to resolve this in the stat constructor before layer(), this will not port well when using stat = "chain" in a geom constructor.
We cannot resolve this during layer() because StatChain$parameters() is unaware of the stats passed as the stats argument due to statelessness. Without editing layer(), there is also no way within layer() to have a stat accept all, arbitrary parameters (to filter later).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: stat executing multiple other stats
1 participant