From d0393aeca1748ac7131bce42bbbd184a27c2b0f7 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 23 Jul 2024 15:00:11 +0200 Subject: [PATCH 1/3] drop empty aesthetics with warning --- R/layer.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/R/layer.R b/R/layer.R index 8b4621bde2..94840f1551 100644 --- a/R/layer.R +++ b/R/layer.R @@ -152,6 +152,15 @@ layer <- function(geom = NULL, stat = NULL, if (any(pattern)) { aes_params[pattern] <- lapply(aes_params[pattern], list) } + # Drop empty aesthetics + empty_aes <- names(aes_params)[lengths(aes_params) == 0] + if (length(empty_aes) > 0) { + cli::cli_warn( + "Ignoring empty aesthetic{?s}: {.arg {empty_aes}}.", + call = call_env + ) + aes_params <- aes_params[setdiff(names(aes_params), empty_aes)] + } # Warn about extra params and aesthetics extra_param <- setdiff(names(params), all) From 03b23fc58b10e51430775135ed311c68a40f566f Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 23 Jul 2024 15:00:20 +0200 Subject: [PATCH 2/3] add test --- tests/testthat/test-layer.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/testthat/test-layer.R b/tests/testthat/test-layer.R index 0a89af1da8..150d6b9496 100644 --- a/tests/testthat/test-layer.R +++ b/tests/testthat/test-layer.R @@ -25,6 +25,13 @@ test_that("unknown aesthetics create warning", { expect_warning(geom_point(aes(blah = "red")), "unknown aesthetics") }) +test_that("empty aesthetics create warning", { + expect_warning( + geom_point(fill = NULL, shape = character()), + "Ignoring empty aesthetics" + ) +}) + test_that("invalid aesthetics throws errors", { # We want to test error and ignore the scale search message suppressMessages({ From c126f14a9ff903d2e878ff3f2d72ea6fcb994233 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 23 Jul 2024 15:01:50 +0200 Subject: [PATCH 3/3] add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 4c0ea1e891..9ddd4c20c2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* Passing empty unmapped aesthetics to layers raises a warning instead of + throwing an error (@teunbrand, #6009). * Missing values from discrete palettes are no longer translated (@teunbrand, #5929). * Fixed bug in `facet_grid(margins = TRUE)` when using expresssions