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

Stats use default_aes prior to computation #6099

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

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #3860.

Briefly, it makes a Stat$use_defaults() method that evaluates Stat$default_aes prior to Stat$setup_data().
This is convenient for filling in optional aesthetics, like weight.

@teunbrand
Copy link
Collaborator Author

Also came across this:

default_aes = aes(colour = "#3366FF", size = 0.5),

Where it seems weird to me that the stat declares aesthetics for the geom.

@thomasp85
Copy link
Member

I think the contract is that default_aes in a Stat means "the default aesthetics to pass on to the geom if not provided by the user. So it is not exactly the same as default_aes in Geom

@teunbrand
Copy link
Collaborator Author

I'm not sure that the contract as you describe it is honoured currently in the CRAN version. Note that the lines are blue, not red.

library(ggplot2)
packageVersion("ggplot2")
#> [1] '3.5.1'

update_stat_defaults(StatDensity2d, aes(colour = "red"))
StatDensity2d$default_aes
#> Aesthetic mapping: 
#> * `colour` -> "red"
#> * `size`   -> 0.5

ggplot(faithful, aes(eruptions, waiting)) +
  stat_density_2d()

With this PR, the updated aesthetic is enforced:

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

update_stat_defaults(StatDensity2d, aes(colour = "red"))

ggplot(faithful, aes(eruptions, waiting)) +
  stat_density_2d()

Created on 2024-12-03 with reprex v2.1.1

@thomasp85
Copy link
Member

My guess is that it doesn't work because the geom "wins". This is also as it should be I think. The stats should not define "graphical" defaults such as colour and the defaults aesthetics for stats are primarily there for aesthetics such as width etc

This is also why they can't be resolved prior to computations as the defaults can rely on values computed by the stat

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.

default_aes not used in compute_*() methods
2 participants