Skip to content

Commit 6c3136a

Browse files
committed
better error message when geom_boxplot() gets data with more than 1 row
1 parent 198bf7a commit 6c3136a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

R/geom-boxplot.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
200200
outlier.alpha = NULL,
201201
notch = FALSE, notchwidth = 0.5, varwidth = FALSE) {
202202

203+
# this may occur when using geom_boxplot(stat = "identity")
204+
if (nrow(data) != 1) {
205+
stop(
206+
"Can't draw more than one boxplot per group. Did you forget aes(group=...)?",
207+
call. = FALSE
208+
)
209+
}
210+
203211
common <- list(
204212
colour = data$colour,
205213
size = data$size,

tests/testthat/test-geom-boxplot.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ test_that("boxes with variable widths do not overlap", {
4747
expect_false(any(duplicated(xid)))
4848
})
4949

50+
test_that("boxplots with a group size >1 error", {
51+
p <- ggplot(
52+
data_frame(x = "one value", y = 3, value = 4:6),
53+
aes(x, ymin = 0, lower = 1, middle = y, upper = value, ymax = 10)
54+
) +
55+
geom_boxplot(stat = "identity")
56+
57+
expect_equal(nrow(layer_data(p, 1)), 3)
58+
expect_error(layer_grob(p, 1), "Can't draw more than one boxplot")
59+
})
5060

5161
# Visual tests ------------------------------------------------------------
5262

0 commit comments

Comments
 (0)