diff --git a/R/geom-boxplot.r b/R/geom-boxplot.r index 93e85a5710..246a0a13f9 100644 --- a/R/geom-boxplot.r +++ b/R/geom-boxplot.r @@ -159,7 +159,7 @@ geom_boxplot <- function(mapping = NULL, data = NULL, #' @export GeomBoxplot <- ggproto("GeomBoxplot", Geom, - # need to declare `width`` here in case this geom is used with a stat that + # need to declare `width` here in case this geom is used with a stat that # doesn't have a `width` parameter (e.g., `stat_identity`). extra_params = c("na.rm", "width"), @@ -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, diff --git a/tests/testthat/test-geom-boxplot.R b/tests/testthat/test-geom-boxplot.R index 32367558dd..99ae3ab511 100644 --- a/tests/testthat/test-geom-boxplot.R +++ b/tests/testthat/test-geom-boxplot.R @@ -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 ------------------------------------------------------------