Skip to content

Commit 65d3bfc

Browse files
authored
follow tidyverse recycling rules for aesthetics (#4670)
1 parent 073c9a0 commit 65d3bfc

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* Aesthetics of length 1 are now recycled to 0 if the length of the data is 0
4+
(@thomasp85, #4588)
5+
36
* Setting `size = NA` will no longer cause `guide_legend()` to error
47
(@thomasp85, #4559)
58

R/geom-.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ NULL
208208
.stroke <- 96 / 25.4
209209

210210
check_aesthetics <- function(x, n) {
211-
ns <- vapply(x, length, numeric(1))
211+
ns <- vapply(x, length, integer(1))
212212
good <- ns == 1L | ns == n
213213

214214
if (all(good)) {

R/layer.r

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ Layer <- ggproto("Layer", NULL,
264264
if (length(evaled) == 0) {
265265
n <- 0
266266
} else {
267-
n <- max(vapply(evaled, length, integer(1)))
267+
aes_n <- vapply(evaled, length, integer(1))
268+
n <- if (min(aes_n) == 0) 0L else max(aes_n)
268269
}
269270
}
270271
check_aesthetics(evaled, n)

tests/testthat/test-aes-setting.r

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ test_that("aesthetic parameters match length of data", {
1313
set_colours(rep("red", 5))
1414
})
1515

16+
test_that("Length 1 aesthetics are recycled to 0", {
17+
p <- ggplot(data.frame(x = numeric(), y = numeric())) +
18+
geom_point(aes(x, y, colour = "red"))
19+
20+
expect_silent(plot(p))
21+
22+
data <- layer_data(p)
23+
24+
expect_equal(nrow(data), 0)
25+
})
26+
1627
test_that("legend filters out aesthetics not of length 1", {
1728
df <- data_frame(x = 1:5, y = 1:5)
1829
p <- ggplot(df, aes(x, y, colour = factor(x))) +

0 commit comments

Comments
 (0)