Skip to content

Commit

Permalink
better error message when geom_boxplot() gets data with more than 1 r…
Browse files Browse the repository at this point in the history
…ow (Fixes tidyverse#3316)
  • Loading branch information
paleolimbot committed May 9, 2019
1 parent 198bf7a commit 8ee5f88
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions R/geom-boxplot.r
Expand Up @@ -200,6 +200,14 @@ GeomBoxplot <- ggproto("GeomBoxplot", Geom,
outlier.alpha = NULL,
notch = FALSE, notchwidth = 0.5, varwidth = FALSE) {

# this may occur when using geom_boxplot(stat = "identity")
if (nrow(data) != 1) {
stop(
"Can't draw more than one boxplot per group. Did you forget aes(group=...)?",
call. = FALSE
)
}

common <- list(
colour = data$colour,
size = data$size,
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-geom-boxplot.R
Expand Up @@ -47,6 +47,16 @@ test_that("boxes with variable widths do not overlap", {
expect_false(any(duplicated(xid)))
})

test_that("boxplots with a group size >1 error", {
p <- ggplot(
data_frame(x = "one value", y = 3, value = 4:6),
aes(x, ymin = 0, lower = 1, middle = y, upper = value, ymax = 10)
) +
geom_boxplot(stat = "identity")

expect_equal(nrow(layer_data(p, 1)), 3)
expect_error(layer_grob(p, 1), "Can't draw more than one boxplot")
})

# Visual tests ------------------------------------------------------------

Expand Down

0 comments on commit 8ee5f88

Please sign in to comment.