Skip to content

Better error message when geom_boxplot() gets group data with more than 1 row #3321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion R/geom-boxplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -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"),

Expand Down 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
Original file line number Diff line number Diff line change
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