diff --git a/NEWS.md b/NEWS.md index 0fd950dcf6..019a4c2e3f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ ### Bug fixes +* Fixed regression where `NULL`-aesthetics contributed to plot labels too + insistently. Now they contribute only as fallback labels (@teunbrand, #6616) * Fixed regression where `draw_key_rect()` stopped using `fill` colours (@mitchelloharawild, #6609). * Fixed regression where `scale_{x,y}_*()` threw an error when an expression diff --git a/R/aes-evaluation.R b/R/aes-evaluation.R index e29d0c5d25..2a7a69d382 100644 --- a/R/aes-evaluation.R +++ b/R/aes-evaluation.R @@ -350,7 +350,10 @@ strip_stage <- function(expr) { make_labels <- function(mapping) { default_label <- function(aesthetic, mapping) { # e.g., geom_smooth(aes(colour = "loess")) or aes(y = NULL) - if (is.null(mapping) || is.atomic(mapping)) { + if (is.null(mapping)) { + return(structure(aesthetic, fallback = TRUE)) + } + if (is.atomic(mapping)) { return(aesthetic) } mapping <- strip_stage(mapping) diff --git a/tests/testthat/test-aes-calculated.R b/tests/testthat/test-aes-calculated.R index 2d389106cf..297ac0b171 100644 --- a/tests/testthat/test-aes-calculated.R +++ b/tests/testthat/test-aes-calculated.R @@ -47,7 +47,11 @@ test_that("make_labels() deparses mappings properly", { expect_match(x_lab, "...$") # if the mapping is a literal or NULL, the aesthetics is used expect_identical(make_labels(aes(x = 1)), list(x = "x")) - expect_identical(make_labels(aes(x = NULL)), list(x = "x")) + # NULL labels should only be used as fallback labels + expect_identical( + make_labels(aes(x = NULL)), + list(x = structure("x", fallback = TRUE)) + ) }) test_that("staged aesthetics warn appropriately for duplicated names", { diff --git a/tests/testthat/test-aes.R b/tests/testthat/test-aes.R index b0922383cc..c3786e3975 100644 --- a/tests/testthat/test-aes.R +++ b/tests/testthat/test-aes.R @@ -103,7 +103,8 @@ test_that("quosures are squashed when creating default label for a mapping", { test_that("labelling doesn't cause error if aesthetic is NULL", { p <- ggplot(mtcars) + aes(x = NULL) labels <- ggplot_build(p)@plot@labels - expect_identical(labels$x, "x") + # NULL labels should only be used as fallback labels + expect_identical(labels$x, structure("x", fallback = TRUE)) }) test_that("aes standardises aesthetic names", {