diff --git a/NEWS.md b/NEWS.md index 3494f9ee7d..19b9c807d2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,9 @@ * `geom_rug()` now works with `coord_flip()` (@has2k1, #2987). +* Default labels are now generated more consistently; e.g., symbols no longer + get backticks, and long expressions are abbreviated with `...` + (@yutannihilation, #2981). # ggplot2 3.1.0 diff --git a/R/aes-calculated.r b/R/aes-calculated.r index 4e16cfe4e6..ff93d8c86e 100644 --- a/R/aes-calculated.r +++ b/R/aes-calculated.r @@ -87,16 +87,19 @@ strip_dots <- function(expr) { # Convert aesthetic mapping into text labels make_labels <- function(mapping) { default_label <- function(aesthetic, mapping) { - # e.g., geom_smooth(aes(colour = "loess")) + # e.g., geom_smooth(aes(colour = "loess")) or aes(y = NULL) if (is.atomic(mapping)) { - aesthetic + return(aesthetic) + } + + mapping <- strip_dots(mapping) + if (rlang::is_quosure(mapping) && rlang::quo_is_symbol(mapping)) { + name <- rlang::as_string(rlang::quo_get_expr(mapping)) } else { - x <- rlang::quo_text(strip_dots(mapping)) - if (length(x) > 1) { - x <- paste0(x[[1]], "...") - } - x + name <- rlang::quo_text(mapping) + name <- gsub("\n.*$", "...", name) } + name } Map(default_label, names(mapping), mapping) } diff --git a/R/plot-construction.r b/R/plot-construction.r index 8a65b58e14..c8ebca0378 100644 --- a/R/plot-construction.r +++ b/R/plot-construction.r @@ -123,7 +123,7 @@ ggplot_add.uneval <- function(object, plot, object_name) { # defaults() doesn't copy class, so copy it. class(plot$mapping) <- class(object) - labels <- lapply(object, function(x) if (is.null(x)) x else rlang::quo_name(x)) + labels <- make_labels(object) names(labels) <- names(object) update_labels(plot, labels) } diff --git a/tests/testthat/test-aes-calculated.r b/tests/testthat/test-aes-calculated.r index f52f319845..8e97dd3ff7 100644 --- a/tests/testthat/test-aes-calculated.r +++ b/tests/testthat/test-aes-calculated.r @@ -24,7 +24,18 @@ test_that("strip_dots remove dots around calculated aesthetics", { ) }) -test_that("calculation stripped from labels", { - expect_equal(make_labels(aes(x = ..y..)), list(x = "y")) - expect_equal(make_labels(aes(x = stat(y))), list(x = "y")) +test_that("make_labels() deprases mappings properly", { + # calculation stripped from labels + expect_identical(make_labels(aes(x = ..y..)), list(x = "y")) + expect_identical(make_labels(aes(x = stat(y))), list(x = "y")) + + # symbol is always deparsed without backticks + expect_identical(make_labels(aes(x = `a b`)), list(x = "a b")) + # long expression is abbreviated with ... + x_lab <- make_labels(aes(x = 2 * x * exp(`coef 1` * x^2) * 2 * x * exp(`coef 1` * x^2) * 2 * x))$x + expect_length(x_lab, 1L) + 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")) }) diff --git a/tests/testthat/test-aes.r b/tests/testthat/test-aes.r index fd36cbc1f0..0ce7c2e43e 100644 --- a/tests/testthat/test-aes.r +++ b/tests/testthat/test-aes.r @@ -95,7 +95,7 @@ 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) - expect_null(p$labels$x) + expect_identical(p$labels$x, "x") }) test_that("aes standardises aesthetic names", {