Skip to content

Commit e3b7822

Browse files
authored
Warn when using constant aesthetics (#5256)
* Add constant check * Exclude geom_{ab/h/v}line from check * Add test * Pass constructor as call in warning
1 parent 66462be commit e3b7822

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

R/geom-abline.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,7 @@ GeomAbline <- ggproto("GeomAbline", Geom,
147147

148148
draw_key = draw_key_abline,
149149

150-
rename_size = TRUE
150+
rename_size = TRUE,
151+
152+
check_constant_aes = FALSE
151153
)

R/geom-hline.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,7 @@ GeomHline <- ggproto("GeomHline", Geom,
6060

6161
draw_key = draw_key_path,
6262

63-
rename_size = TRUE
63+
rename_size = TRUE,
64+
65+
check_constant_aes = FALSE
6466
)

R/geom-vline.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,7 @@ GeomVline <- ggproto("GeomVline", Geom,
6060

6161
draw_key = draw_key_vline,
6262

63-
rename_size = TRUE
63+
rename_size = TRUE,
64+
65+
check_constant_aes = FALSE
6466
)

R/layer.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,22 @@ Layer <- ggproto("Layer", NULL,
292292
}
293293

294294
n <- nrow(data)
295+
aes_n <- lengths(evaled)
295296
if (n == 0) {
296297
# No data, so look at longest evaluated aesthetic
297298
if (length(evaled) == 0) {
298299
n <- 0
299300
} else {
300-
aes_n <- lengths(evaled)
301301
n <- if (min(aes_n) == 0) 0L else max(aes_n)
302302
}
303303
}
304+
if ((self$geom$check_constant_aes %||% TRUE)
305+
&& length(aes_n) > 0 && all(aes_n == 1) && n > 1) {
306+
cli::cli_warn(c(
307+
"All aesthetics have length 1, but the data has {n} rows.",
308+
i = "Did you mean to use {.fn annotate}?"
309+
), call = self$constructor)
310+
}
304311
check_aesthetics(evaled, n)
305312

306313
# Set special group and panel vars

tests/testthat/_snaps/layer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
! Can only draw one boxplot per group
8585
i Did you forget `aes(group = ...)`?
8686

87+
# layer warns for constant aesthetics
88+
89+
All aesthetics have length 1, but the data has 32 rows.
90+
i Did you mean to use `annotate()`?
91+
8792
# layer_data returns a data.frame
8893

8994
`layer_data()` must return a <data.frame>

tests/testthat/test-layer.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ test_that("layer reports the error with correct index etc", {
123123
expect_snapshot_error(ggplotGrob(p))
124124
})
125125

126+
test_that("layer warns for constant aesthetics", {
127+
p <- ggplot(mtcars, aes(x = seq_along(mpg))) + geom_point(aes(y = 2))
128+
expect_silent(ggplot_build(p))
129+
130+
p <- ggplot(mtcars, aes(x = 1)) + geom_point(aes(y = 2))
131+
expect_snapshot_warning(ggplot_build(p))
132+
})
133+
126134
# Data extraction ---------------------------------------------------------
127135

128136
test_that("layer_data returns a data.frame", {

0 commit comments

Comments
 (0)