Skip to content
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
not passed as the `name` argument (@teunbrand, #6623)
* Fixed issue where vectorised `arrow()`s caused errors in drawing the
legend glyphs (@teunbrand, #6594)
* Fixed regression where `NULL`-aesthetics contributed to plot labels too
insistently. Now they contribute only as fallback labels (@teunbrand, #6616)

# ggplot2 4.0.0

Expand Down
20 changes: 0 additions & 20 deletions R/aes-evaluation.R
Original file line number Diff line number Diff line change
Expand Up @@ -346,26 +346,6 @@ strip_stage <- function(expr) {
}
}

# Convert aesthetic mapping into text labels
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)) {
return(aesthetic)
}
mapping <- strip_stage(mapping)
mapping <- strip_dots(mapping, strip_pronoun = TRUE)
if (is_quosure(mapping) && quo_is_symbol(mapping)) {
name <- as_string(quo_get_expr(mapping))
} else {
name <- quo_text(mapping)
name <- gsub("\n.*$", "...", name)
}
name
}
Map(default_label, names(mapping), mapping)
}

eval_aesthetics <- function(aesthetics, data, mask = NULL) {

env <- child_env(base_env())
Expand Down
20 changes: 20 additions & 0 deletions R/labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,26 @@ setup_plot_labels <- function(plot, layers, data) {
labs(!!!defaults(plot_labels, labels))
}

# Convert aesthetic mapping into text labels
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)) {
return(structure(aesthetic, fallback = TRUE))
}
mapping <- strip_stage(mapping)
mapping <- strip_dots(mapping, strip_pronoun = TRUE)
if (is_quosure(mapping) && quo_is_symbol(mapping)) {
name <- as_string(quo_get_expr(mapping))
} else {
name <- quo_text(mapping)
name <- gsub("\n.*$", "...", name)
}
name
}
Map(default_label, names(mapping), mapping)
}

#' Modify axis, legend, and plot labels
#'
#' Good labels are critical for making your plots accessible to a wider
Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/test-aes-calculated.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ test_that("make_labels() deparses mappings properly", {
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"))
fallback <- list(x = structure("x", fallback = TRUE))
# if the mapping is a literal or NULL, the aesthetics is used as fallback
expect_identical(make_labels(aes(x = 1)), fallback)
# NULL labels should only be used as fallback labels
expect_identical(make_labels(aes(x = NULL)), fallback)
})

test_that("staged aesthetics warn appropriately for duplicated names", {
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-aes.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,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", {
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-draw-key.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ test_that("keep_draw_key", {
aes(colour = "line", alpha = "line"),
show.legend = c("colour" = NA, alpha = TRUE)
) +
suppressWarnings(scale_alpha_discrete())
suppressWarnings(scale_alpha_discrete()) +
guides(
alpha = guide_legend(order = 1),
colour = guide_legend(order = 2)
)

expect_doppelganger("appropriate colour key with alpha key as lines", p)

Expand Down