Skip to content

Make make_labels() more consistent #2981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 10 additions & 7 deletions R/aes-calculated.r
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion R/plot-construction.r
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
17 changes: 14 additions & 3 deletions tests/testthat/test-aes-calculated.r
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
2 changes: 1 addition & 1 deletion tests/testthat/test-aes.r
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down