diff --git a/NEWS.md b/NEWS.md index 34750ee04c..c1d127b555 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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()` diff --git a/R/geom-.R b/R/geom-.R index bb409ba0cf..dae3027e1b 100644 --- a/R/geom-.R +++ b/R/geom-.R @@ -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) diff --git a/tests/testthat/test-aes-calculated.R b/tests/testthat/test-aes-calculated.R index ac77df4c48..2d389106cf 100644 --- a/tests/testthat/test-aes-calculated.R +++ b/tests/testthat/test-aes-calculated.R @@ -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")) +})