Skip to content

Commit 28ca00c

Browse files
authored
Warn about simultaneous use of mapped and static aesthetics (#6743)
* mapped aesthetics as variable * all aesthetics as variable * add warning * add test
1 parent 1979167 commit 28ca00c

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

R/layer.R

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ layer <- function(geom = NULL, stat = NULL,
5959
if (!is.null(mapping)) {
6060
mapping <- validate_mapping(mapping, call_env)
6161
}
62+
mapped_aes <- mapped_aesthetics(mapping)
6263

6364
data <- fortify(data)
6465

@@ -70,13 +71,14 @@ layer <- function(geom = NULL, stat = NULL,
7071
params$na.rm <- params$na.rm %||% FALSE
7172

7273
# Split up params between aesthetics, geom, and stat
74+
all_aes <- unique(c(geom$aesthetics(), position$aesthetics(), stat$aesthetics()))
7375
params <- rename_aes(params)
74-
aes_params <- params[intersect(names(params), union(geom$aesthetics(), position$aesthetics()))]
76+
aes_params <- params[intersect(names(params), all_aes)]
7577
geom_params <- params[intersect(names(params), geom$parameters(TRUE))]
7678
stat_params <- params[intersect(names(params), stat$parameters(TRUE))]
7779

7880
ignore <- c("key_glyph", "name", "layout")
79-
all <- c(geom$parameters(TRUE), stat$parameters(TRUE), geom$aesthetics(), position$aesthetics(), ignore)
81+
all <- c(geom$parameters(TRUE), stat$parameters(TRUE), all_aes, ignore)
8082

8183
# Take care of plain patterns provided as aesthetic
8284
pattern <- vapply(aes_params, is_pattern, logical(1))
@@ -87,21 +89,31 @@ layer <- function(geom = NULL, stat = NULL,
8789
# Warn about extra params and aesthetics
8890
extra_param <- setdiff(names(params), all)
8991
# Take care of size->linewidth renaming in layer params
90-
if (geom$rename_size && "size" %in% extra_param && !"linewidth" %in% mapped_aesthetics(mapping)) {
92+
if (geom$rename_size && "size" %in% extra_param && !"linewidth" %in% mapped_aes) {
9193
aes_params <- c(aes_params, params["size"])
9294
extra_param <- setdiff(extra_param, "size")
9395
deprecate("3.4.0", I("Using `size` aesthetic for lines"), I("`linewidth`"), user_env = user_env)
9496
}
95-
if (check.param && length(extra_param) > 0) {
96-
cli::cli_warn("Ignoring unknown parameters: {.arg {extra_param}}", call = call_env)
97+
if (check.param) {
98+
if (length(extra_param) > 0) {
99+
cli::cli_warn("Ignoring unknown parameters: {.arg {extra_param}}", call = call_env)
100+
}
101+
double_defined <- intersect(mapped_aes, names(aes_params))
102+
if (length(double_defined) > 0) {
103+
cli::cli_warn(
104+
c(
105+
"The {.and {.field {double_defined}}} aesthetic{?s} {?is/are} \\
106+
defined twice: once in {.arg mapping} and once as a static aesthetic.",
107+
"i" = "The static aesthetic overrules the mapped aesthetic."
108+
),
109+
call = call_env
110+
)
111+
}
97112
}
98113

99-
extra_aes <- setdiff(
100-
mapped_aesthetics(mapping),
101-
c(geom$aesthetics(), stat$aesthetics(), position$aesthetics())
102-
)
114+
extra_aes <- setdiff(mapped_aes, all_aes)
103115
# Take care of size->linewidth aes renaming
104-
if (geom$rename_size && "size" %in% extra_aes && !"linewidth" %in% mapped_aesthetics(mapping)) {
116+
if (geom$rename_size && "size" %in% extra_aes && !"linewidth" %in% mapped_aes) {
105117
extra_aes <- setdiff(extra_aes, "size")
106118
deprecate("3.4.0", I("Using `size` aesthetic for lines"), I("`linewidth`"), user_env = user_env)
107119
}

tests/testthat/_snaps/layer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747

4848
Ignoring empty aesthetics: `fill` and `shape`.
4949

50+
# aesthetics defined twice create warning
51+
52+
The size aesthetic is defined twice: once in `mapping` and once as a static aesthetic.
53+
i The static aesthetic overrules the mapped aesthetic.
54+
5055
# invalid aesthetics throws errors
5156

5257
Problem while computing aesthetics.

tests/testthat/test-layer.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ test_that("empty aesthetics create warning", {
3333
expect_snapshot_warning(ggplot_build(p))
3434
})
3535

36+
test_that("aesthetics defined twice create warning", {
37+
expect_snapshot_warning(geom_point(aes(size = foo), size = 12))
38+
})
39+
3640
test_that("invalid aesthetics throws errors", {
3741
# We want to test error and ignore the scale search message
3842
suppressMessages({

0 commit comments

Comments
 (0)