Skip to content

Commit 7fb4c38

Browse files
authored
Exclude labels from aes_params field (#6004)
* exclude labels from `aes_params` * add test
1 parent 25ad0b1 commit 7fb4c38

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

R/labels.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ setup_plot_labels <- function(plot, layers, data) {
2424
# Find labels from every layer
2525
for (i in seq_along(layers)) {
2626
layer <- layers[[i]]
27+
exclude <- names(layer$aes_params)
2728
mapping <- layer$computed_mapping
2829
mapping <- strip_stage(mapping)
2930
mapping <- strip_dots(mapping, strip_pronoun = TRUE)
31+
mapping <- mapping[setdiff(names(mapping), exclude)]
3032

3133
# Acquire default labels
3234
mapping_default <- make_labels(mapping)

tests/testthat/test-labels.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ test_that("Labels can be extracted from attributes", {
7474
expect_equal(labels$y, "disp")
7575
})
7676

77+
test_that("Labels from static aesthetics are ignored (#6003)", {
78+
79+
df <- data.frame(x = 1, y = 1, f = 1)
80+
81+
p <- ggplot(df, aes(x, y, colour = f)) + geom_point()
82+
labels <- ggplot_build(p)$plot$labels
83+
84+
expect_equal(labels$colour, "f")
85+
86+
p <- ggplot(df, aes(x, y, colour = f)) + geom_point(colour = "blue")
87+
labels <- ggplot_build(p)$plot$labels
88+
89+
expect_null(labels$colour)
90+
})
91+
7792
test_that("alt text is returned", {
7893
p <- ggplot(mtcars, aes(mpg, disp)) +
7994
geom_point()

0 commit comments

Comments
 (0)