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

after_scale() does not work in Geom$default_aes field #6135

Open
teunbrand opened this issue Oct 11, 2024 · 1 comment · May be fixed by #6137
Open

after_scale() does not work in Geom$default_aes field #6135

teunbrand opened this issue Oct 11, 2024 · 1 comment · May be fixed by #6137

Comments

@teunbrand
Copy link
Collaborator

This was brought to my attention by @larmarange.

Hypothetically, one could want to automate making the following plot, by setting the default colour aesthetic in the geom.

library(ggplot2)

ggplot(mpg, aes(displ, hwy, fill = drv)) +
  geom_point(aes(colour = after_scale(alpha(fill, 0.4))))

To do so, it does not seem unreasonable to make colour = after_scale(alpha(fill, 0.4)) part of the Geom$default_aes field.

GeomPointAlt <- ggproto(
  "GeomPointAlt", GeomPoint,
  default_aes = aes(
    colour = after_scale(alpha(fill, 0.4)),
    # Merge with default, removing pre-existing `colour`
    !!!modifyList(GeomPoint$default_aes, list(colour = NULL))
  )
)

However, that doesn't work because default aesthetics are evaluated in isolation, not in context of the data. For that reason, the fill aesthetic cannot be found.

ggplot(mpg, aes(displ, hwy, fill = drv)) +
  stat_identity(geom = GeomPointAlt)
#> Warning: Failed to apply `after_scale()` modifications to legend
#> Caused by error:
#> ! object 'fill' not found
#> Error: object 'fill' not found

Created on 2024-10-11 with reprex v2.1.1

The relevant line is indicated below, and I think we only need to add a data = data to the lapply().

missing_eval <- lapply(default_aes, eval_tidy)

@yjunechoe
Copy link
Contributor

yjunechoe commented Oct 11, 2024

BTW I don't know if this will complicate things a lot, but I think get_geom_defaults() which also errors on that line will need a special treatment since data = NULL. (or maybe asking for a "default" value for delayed aesthetics should just error/skip early as that may be kinda odd)

get_geom_defaults(GeomPointAlt)
#> Error: object 'fill' not found

@teunbrand teunbrand linked a pull request Oct 15, 2024 that will close this issue
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 a pull request may close this issue.

2 participants