From bd2bd1ef6260dfd7f783a3652720d614f97243a8 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 14 Oct 2024 09:25:12 +0200 Subject: [PATCH 1/4] append to modifiers --- R/geom-.R | 7 +++++++ 1 file changed, 7 insertions(+) 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) From 74703cbfd879103c594084dddaf50eed2802a405 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 14 Oct 2024 09:25:17 +0200 Subject: [PATCH 2/4] add test --- tests/testthat/test-aes-calculated.R | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/testthat/test-aes-calculated.R b/tests/testthat/test-aes-calculated.R index 9d5c49c68e..c3d52819cc 100644 --- a/tests/testthat/test-aes-calculated.R +++ b/tests/testthat/test-aes-calculated.R @@ -148,3 +148,23 @@ 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))) + ) + ) + + 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')) +}) From 5b1565d3fa38a1ae9dee23450ade86a67d6d26a2 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 14 Oct 2024 09:48:57 +0200 Subject: [PATCH 3/4] include test for `get_geom_defaults()` --- tests/testthat/test-aes-calculated.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-aes-calculated.R b/tests/testthat/test-aes-calculated.R index c3d52819cc..536a458a25 100644 --- a/tests/testthat/test-aes-calculated.R +++ b/tests/testthat/test-aes-calculated.R @@ -155,7 +155,7 @@ test_that("A geom can have scaled defaults (#6135)", { NULL, GeomPoint, default_aes = modify_list( GeomPoint$default_aes, - aes(colour = after_scale(alpha(fill, 0.5))) + aes(colour = after_scale(alpha(fill, 0.5)), fill = "black") ) ) @@ -167,4 +167,7 @@ test_that("A geom can have scaled defaults (#6135)", { ) expect_equal(ld$colour, c("#FF000080", "#00FF0080", '#0000FF80')) + + defaults <- get_geom_defaults(test_geom) + expect_equal(defaults$colour, c("#00000080")) }) From 60f1516ccf1b47940c92c77d731180ea641161d7 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 15 Oct 2024 09:15:32 +0200 Subject: [PATCH 4/4] add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index f369879868..b6de90f617 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 the `ggplot2::`-prefix did not work with `stage()` (@teunbrand, #6104). * New `get_labs()` function for retrieving completed plot labels