Skip to content

Commit 7364e13

Browse files
paleolimbotclauswilke
authored andcommitted
Better error message when geom_boxplot() gets group data with more than 1 row (#3321)
* fix typo in geom_boxplot comment * better error message when geom_boxplot() gets data with more than 1 row (Fixes #3316) * Fix typo, Fixes #3316
1 parent 3b69172 commit 7364e13

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

R/geom-boxplot.r

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
159159
#' @export
160160
GeomBoxplot <- ggproto("GeomBoxplot", Geom,
161161

162-
# need to declare `width`` here in case this geom is used with a stat that
162+
# need to declare `width` here in case this geom is used with a stat that
163163
# doesn't have a `width` parameter (e.g., `stat_identity`).
164164
extra_params = c("na.rm", "width"),
165165

@@ -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)