From fb872e84ae885f651ac71fc02d70b91b28825193 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Tue, 28 Dec 2021 23:40:22 +0100 Subject: [PATCH 1/5] Fix duplicated aesthetics warning with multiple modified aes --- R/geom-.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/geom-.r b/R/geom-.r index e513769c03..48ec87ce8f 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) From e954f6c954b0ebcc1d676614db48278ead56cedb Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 16 Mar 2022 00:09:47 +0100 Subject: [PATCH 2/5] Bump rlang and testthat version requirements --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c45379a5ff..10c5f9a959 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -40,7 +40,7 @@ Imports: lifecycle, MASS, mgcv, - rlang (>= 0.4.10), + rlang (>= 1.0.0), scales (>= 0.5.0), stats, tibble, @@ -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: From 6c27fed5d9703e41f8d8d82f792f572a630c0a3b Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 16 Mar 2022 00:13:59 +0100 Subject: [PATCH 3/5] add warning message snapshot --- tests/testthat/_snaps/aes-calculated.md | 8 ++++++++ tests/testthat/test-aes-calculated.r | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/testthat/_snaps/aes-calculated.md 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..5e48fe8fe0 100644 --- a/tests/testthat/test-aes-calculated.r +++ b/tests/testthat/test-aes-calculated.r @@ -49,3 +49,19 @@ 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") + expr <- substitute( + ggplot(df, aes(x, y, label = lab)) + + geom_label( + aes(colour = stage(lab, after_scale = colour), + color = after_scale(color)) + ) + ) + # One warning in plot code due to evaluation of `aes()` + expect_snapshot_warning(p <- eval(expr)) + # Two warnings in building due to `stage()`/`after_scale()` + expect_snapshot_warning(ggplot_build(p)) +}) From de58b45a2e4e639146f55b3e32af45068a9c55b1 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 16 Mar 2022 20:50:40 +0100 Subject: [PATCH 4/5] improve test --- tests/testthat/test-aes-calculated.r | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/testthat/test-aes-calculated.r b/tests/testthat/test-aes-calculated.r index 5e48fe8fe0..eacf21b884 100644 --- a/tests/testthat/test-aes-calculated.r +++ b/tests/testthat/test-aes-calculated.r @@ -53,15 +53,19 @@ test_that("make_labels() deprases mappings properly", { 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") - expr <- substitute( - ggplot(df, aes(x, y, label = lab)) + + + # 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 plot code due to evaluation of `aes()` - expect_snapshot_warning(p <- eval(expr)) - # Two warnings in building due to `stage()`/`after_scale()` + # One warning in building due to `stage()`/`after_scale()` expect_snapshot_warning(ggplot_build(p)) }) From 0143602572d387b88beaaddaff3f6e3d6e5eb242 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 16 Mar 2022 23:49:07 +0100 Subject: [PATCH 5/5] update NEWS.md --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 64dc53b67c..5dcfc09636 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). + * `annotate()` now documents unsupported geoms (`geom_abline()`, `geom_hline()` and `geom_vline()`), and warns when they are requested (@mikmart, #4719)