Skip to content

Commit e69687a

Browse files
authored
Ignore empty non-mapped aesthetics (#6011)
* drop empty aesthetics with warning * add test * add news bullet
1 parent 8bb9641 commit e69687a

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* Passing empty unmapped aesthetics to layers raises a warning instead of
4+
throwing an error (@teunbrand, #6009).
35
* Moved {mgcv} from Imports to Suggests (@teunbrand, #5986)
46
* New `reset_geom_defaults()` and `reset_stat_defaults()` to restore all geom or
57
stat default aesthetics at once (@teunbrand, #5975).

R/layer.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ layer <- function(geom = NULL, stat = NULL,
146146
if (any(pattern)) {
147147
aes_params[pattern] <- lapply(aes_params[pattern], list)
148148
}
149+
# Drop empty aesthetics
150+
empty_aes <- names(aes_params)[lengths(aes_params) == 0]
151+
if (length(empty_aes) > 0) {
152+
cli::cli_warn(
153+
"Ignoring empty aesthetic{?s}: {.arg {empty_aes}}.",
154+
call = call_env
155+
)
156+
aes_params <- aes_params[setdiff(names(aes_params), empty_aes)]
157+
}
149158

150159
# Warn about extra params and aesthetics
151160
extra_param <- setdiff(names(params), all)

tests/testthat/test-layer.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ test_that("unknown aesthetics create warning", {
2525
expect_warning(geom_point(aes(blah = "red")), "unknown aesthetics")
2626
})
2727

28+
test_that("empty aesthetics create warning", {
29+
expect_warning(
30+
geom_point(fill = NULL, shape = character()),
31+
"Ignoring empty aesthetics"
32+
)
33+
})
34+
2835
test_that("invalid aesthetics throws errors", {
2936
# We want to test error and ignore the scale search message
3037
suppressMessages({

0 commit comments

Comments
 (0)