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

Using after_scale() in Geom$default_aes field #6137

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* (internal) Using `after_scale()` in the `Geom*$default_aes()` field is now
evaluated in the context of data (@teunbrand, #6135)
* Fixed bug where binned scales wouldn't simultaneously accept transformations
and function-limits (@teunbrand, #6144).
* Fixed bug where the `ggplot2::`-prefix did not work with `stage()`
Expand Down
7 changes: 7 additions & 0 deletions R/geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ Geom <- ggproto("Geom",
themed_defaults <- eval_from_theme(default_aes, theme)
default_aes[names(themed_defaults)] <- themed_defaults

# Mark staged/scaled defaults as modifier (#6135)
delayed <- is_scaled_aes(default_aes) | is_staged_aes(default_aes)
if (any(delayed)) {
modifiers <- defaults(modifiers, default_aes[delayed])
default_aes <- default_aes[!delayed]
}

missing_eval <- lapply(default_aes, eval_tidy)
# Needed for geoms with defaults set to NULL (e.g. GeomSf)
missing_eval <- compact(missing_eval)
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-aes-calculated.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,26 @@ test_that("stage allows aesthetics that are only mapped to start", {
)

})

test_that("A geom can have scaled defaults (#6135)", {

test_geom <- ggproto(
NULL, GeomPoint,
default_aes = modify_list(
GeomPoint$default_aes,
aes(colour = after_scale(alpha(fill, 0.5)), fill = "black")
)
)

df <- data.frame(x = 1:3, fill = c("#FF0000", "#00FF00", "#0000FF"))

ld <- layer_data(
ggplot(df, aes(x, x, fill = I(fill))) +
stat_identity(geom = test_geom)
)

expect_equal(ld$colour, c("#FF000080", "#00FF0080", '#0000FF80'))

defaults <- get_geom_defaults(test_geom)
expect_equal(defaults$colour, c("#00000080"))
})
Loading