Skip to content

Commit 868fdb7

Browse files
Make make_labels() more consistent (#2981)
1 parent 67ab63d commit 868fdb7

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
* `geom_rug()` now works with `coord_flip()` (@has2k1, #2987).
44

5+
* Default labels are now generated more consistently; e.g., symbols no longer
6+
get backticks, and long expressions are abbreviated with `...`
7+
(@yutannihilation, #2981).
58

69
# ggplot2 3.1.0
710

R/aes-calculated.r

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,19 @@ strip_dots <- function(expr) {
8787
# Convert aesthetic mapping into text labels
8888
make_labels <- function(mapping) {
8989
default_label <- function(aesthetic, mapping) {
90-
# e.g., geom_smooth(aes(colour = "loess"))
90+
# e.g., geom_smooth(aes(colour = "loess")) or aes(y = NULL)
9191
if (is.atomic(mapping)) {
92-
aesthetic
92+
return(aesthetic)
93+
}
94+
95+
mapping <- strip_dots(mapping)
96+
if (rlang::is_quosure(mapping) && rlang::quo_is_symbol(mapping)) {
97+
name <- rlang::as_string(rlang::quo_get_expr(mapping))
9398
} else {
94-
x <- rlang::quo_text(strip_dots(mapping))
95-
if (length(x) > 1) {
96-
x <- paste0(x[[1]], "...")
97-
}
98-
x
99+
name <- rlang::quo_text(mapping)
100+
name <- gsub("\n.*$", "...", name)
99101
}
102+
name
100103
}
101104
Map(default_label, names(mapping), mapping)
102105
}

R/plot-construction.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ ggplot_add.uneval <- function(object, plot, object_name) {
123123
# defaults() doesn't copy class, so copy it.
124124
class(plot$mapping) <- class(object)
125125

126-
labels <- lapply(object, function(x) if (is.null(x)) x else rlang::quo_name(x))
126+
labels <- make_labels(object)
127127
names(labels) <- names(object)
128128
update_labels(plot, labels)
129129
}

tests/testthat/test-aes-calculated.r

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,18 @@ test_that("strip_dots remove dots around calculated aesthetics", {
2424
)
2525
})
2626

27-
test_that("calculation stripped from labels", {
28-
expect_equal(make_labels(aes(x = ..y..)), list(x = "y"))
29-
expect_equal(make_labels(aes(x = stat(y))), list(x = "y"))
27+
test_that("make_labels() deprases mappings properly", {
28+
# calculation stripped from labels
29+
expect_identical(make_labels(aes(x = ..y..)), list(x = "y"))
30+
expect_identical(make_labels(aes(x = stat(y))), list(x = "y"))
31+
32+
# symbol is always deparsed without backticks
33+
expect_identical(make_labels(aes(x = `a b`)), list(x = "a b"))
34+
# long expression is abbreviated with ...
35+
x_lab <- make_labels(aes(x = 2 * x * exp(`coef 1` * x^2) * 2 * x * exp(`coef 1` * x^2) * 2 * x))$x
36+
expect_length(x_lab, 1L)
37+
expect_match(x_lab, "...$")
38+
# if the mapping is a literal or NULL, the aesthetics is used
39+
expect_identical(make_labels(aes(x = 1)), list(x = "x"))
40+
expect_identical(make_labels(aes(x = NULL)), list(x = "x"))
3041
})

tests/testthat/test-aes.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ test_that("quosures are squashed when creating default label for a mapping", {
9595

9696
test_that("labelling doesn't cause error if aesthetic is NULL", {
9797
p <- ggplot(mtcars) + aes(x = NULL)
98-
expect_null(p$labels$x)
98+
expect_identical(p$labels$x, "x")
9999
})
100100

101101
test_that("aes standardises aesthetic names", {

0 commit comments

Comments
 (0)