diff --git a/DESCRIPTION b/DESCRIPTION index a3b7027ce4..10c5f9a959 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -69,7 +69,7 @@ Suggests: rpart, sf (>= 0.7-3), svglite (>= 1.2.0.9001), - testthat (>= 3.0.0), + testthat (>= 3.1.2), vdiffr (>= 1.0.0), xml2 Enhances: diff --git a/NEWS.md b/NEWS.md index 30e0f0c096..7d578b02f2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # ggplot2 (development version) +* Using multiple modified aesthetics correctly will no longer trigger warnings. + If used incorrectly, the warning will now report the duplicated aesthetic + instead of `NA` (@teunbrand, #4707). + * `...` supports `rlang::list2` dynamic dots in all public functions. (@mone27, #4764) * `theme()` now has a `strip.clip` argument, that can be set to `"off"` to diff --git a/R/geom-.r b/R/geom-.r index 8265d6cd4c..18a223e5a4 100644 --- a/R/geom-.r +++ b/R/geom-.r @@ -143,7 +143,7 @@ Geom <- ggproto("Geom", abort(msg) } - names(modified_aes) <- rename_aes(names(modifiers)) + names(modified_aes) <- names(rename_aes(modifiers)) modified_aes <- new_data_frame(compact(modified_aes)) data <- cunion(modified_aes, data) diff --git a/tests/testthat/_snaps/aes-calculated.md b/tests/testthat/_snaps/aes-calculated.md new file mode 100644 index 0000000000..87450bfb04 --- /dev/null +++ b/tests/testthat/_snaps/aes-calculated.md @@ -0,0 +1,8 @@ +# staged aesthetics warn appropriately for duplicated names + + Duplicated aesthetics after name standardisation: colour + +--- + + Duplicated aesthetics after name standardisation: colour + diff --git a/tests/testthat/test-aes-calculated.r b/tests/testthat/test-aes-calculated.r index 8377eea5fa..eacf21b884 100644 --- a/tests/testthat/test-aes-calculated.r +++ b/tests/testthat/test-aes-calculated.r @@ -49,3 +49,23 @@ test_that("make_labels() deprases mappings properly", { expect_identical(make_labels(aes(x = 1)), list(x = "x")) expect_identical(make_labels(aes(x = NULL)), list(x = "x")) }) + +test_that("staged aesthetics warn appropriately for duplicated names", { + # Test should *not* report `NA` as the duplicated aes (#4707) + df <- data.frame(x = 1, y = 1, lab = "test") + + # One warning in plot code due to evaluation of `aes()` + expect_snapshot_warning( + p <- ggplot(df, aes(x, y, label = lab)) + + geom_label( + aes(colour = stage(lab, after_scale = colour), + color = after_scale(color)) + ) + + # Guide would trigger another warning when plot is printed, due to the + # `guide_geom.legend` also using `Geom$use_defaults` method, which we + # test next + guides(colour = "none") + ) + # One warning in building due to `stage()`/`after_scale()` + expect_snapshot_warning(ggplot_build(p)) +})