Skip to content

Commit

Permalink
Exclude labels from aes_params field (#6004)
Browse files Browse the repository at this point in the history
* exclude labels from `aes_params`

* add test
  • Loading branch information
teunbrand committed Jul 17, 2024
1 parent 25ad0b1 commit 7fb4c38
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions R/labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ setup_plot_labels <- function(plot, layers, data) {
# Find labels from every layer
for (i in seq_along(layers)) {
layer <- layers[[i]]
exclude <- names(layer$aes_params)
mapping <- layer$computed_mapping
mapping <- strip_stage(mapping)
mapping <- strip_dots(mapping, strip_pronoun = TRUE)
mapping <- mapping[setdiff(names(mapping), exclude)]

# Acquire default labels
mapping_default <- make_labels(mapping)
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ test_that("Labels can be extracted from attributes", {
expect_equal(labels$y, "disp")
})

test_that("Labels from static aesthetics are ignored (#6003)", {

df <- data.frame(x = 1, y = 1, f = 1)

p <- ggplot(df, aes(x, y, colour = f)) + geom_point()
labels <- ggplot_build(p)$plot$labels

expect_equal(labels$colour, "f")

p <- ggplot(df, aes(x, y, colour = f)) + geom_point(colour = "blue")
labels <- ggplot_build(p)$plot$labels

expect_null(labels$colour)
})

test_that("alt text is returned", {
p <- ggplot(mtcars, aes(mpg, disp)) +
geom_point()
Expand Down

0 comments on commit 7fb4c38

Please sign in to comment.