Skip to content

Commit f672845

Browse files
authored
Skip annotation labels (#6322)
* class for skipping labels * skip unlabelled_uneval class * prevent re-classing upon validation * add test
1 parent f6ef0c1 commit f672845

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

R/aes.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ aes_all <- function(vars) {
360360
# refer to the data mask
361361
structure(
362362
lapply(vars, function(x) new_quosure(as.name(x), emptyenv())),
363-
class = "uneval"
363+
class = c("unlabelled_uneval", "uneval")
364364
)
365365
}
366366

R/labels.R

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ 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)
27+
2828
mapping <- layer$computed_mapping
29+
if (inherits(mapping, "unlabelled_uneval")) {
30+
next
31+
}
32+
2933
mapping <- strip_stage(mapping)
3034
mapping <- strip_dots(mapping, strip_pronoun = TRUE)
35+
36+
exclude <- names(layer$aes_params)
3137
mapping <- mapping[setdiff(names(mapping), exclude)]
3238

3339
# Acquire default labels

R/layer.R

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ validate_mapping <- function(mapping, call = caller_env()) {
210210
}
211211

212212
cli::cli_abort(msg, call = call)
213+
} else {
214+
return(mapping)
213215
}
214216

215217
# For backward compatibility with pre-tidy-eval layers

tests/testthat/test-labels.R

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ test_that("Labels from static aesthetics are ignored (#6003)", {
8383
expect_null(get_labs(p)$colour)
8484
})
8585

86+
test_that("Labels from annotations are ignored (#6316)", {
87+
df <- data.frame(a = 1, b = 2)
88+
p <- ggplot(df, aes(a, b)) + annotate("point", x = 1, y = 2) + geom_point()
89+
labs <- get_labs(p)
90+
expect_equal(labs[c("x", "y")], list(x = "a", y = "b"))
91+
})
92+
8693
test_that("alt text is returned", {
8794
p <- ggplot(mtcars, aes(mpg, disp)) +
8895
geom_point()

0 commit comments

Comments
 (0)