From f408588c5201e2275c8ec8d1bf70b7e419d0ad61 Mon Sep 17 00:00:00 2001 From: Sierra Johnson Date: Fri, 19 Sep 2025 08:58:57 -0600 Subject: [PATCH 1/3] add warning when orientation is ambiguous fixes #4250 --- R/stat-boxplot.R | 8 +++++++- R/utilities.R | 2 +- tests/testthat/_snaps/geom-boxplot.md | 9 +++------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/R/stat-boxplot.R b/R/stat-boxplot.R index 41819e5e1e..cf4fc1b882 100644 --- a/R/stat-boxplot.R +++ b/R/stat-boxplot.R @@ -23,7 +23,13 @@ StatBoxplot <- ggproto("StatBoxplot", Stat, setup_params = function(self, data, params) { params$flipped_aes <- has_flipped_aes(data, params, main_is_orthogonal = TRUE, group_has_equal = TRUE, - main_is_optional = TRUE) + main_is_optional = TRUE, + default = NA) + + if (is.na(params$flipped_aes)) { + cli::cli_warn("Warning: Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'.") + params$flipped_aes <- FALSE + } data <- flip_data(data, params$flipped_aes) has_x <- !(is.null(data$x) && is.null(params$x)) diff --git a/R/utilities.R b/R/utilities.R index 348430fd12..be98b47568 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -562,7 +562,7 @@ has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA, } } - isTRUE(default) + default } #' @rdname bidirection #' @export diff --git a/tests/testthat/_snaps/geom-boxplot.md b/tests/testthat/_snaps/geom-boxplot.md index 10e75bb438..213ae11e5a 100644 --- a/tests/testthat/_snaps/geom-boxplot.md +++ b/tests/testthat/_snaps/geom-boxplot.md @@ -1,17 +1,14 @@ # geom_boxplot for continuous x gives warning if more than one x (#992) - Continuous x aesthetic - i did you forget `aes(group = ...)`? + Warning: Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'. --- - Continuous x aesthetic - i did you forget `aes(group = ...)`? + Warning: Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'. --- - Continuous x aesthetic - i did you forget `aes(group = ...)`? + Warning: Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'. # boxplots with a group size >1 error From b6449ee3a1cca3ff9f000131b04e6405b4d0519d Mon Sep 17 00:00:00 2001 From: Sierra Johnson Date: Fri, 19 Sep 2025 09:31:03 -0600 Subject: [PATCH 2/3] update snaps --- R/stat-boxplot.R | 2 +- tests/testthat/_snaps/geom-boxplot.md | 27 ++++++++++++++++++++++++--- tests/testthat/test-geom-boxplot.R | 6 +++--- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/R/stat-boxplot.R b/R/stat-boxplot.R index cf4fc1b882..ff51b8354d 100644 --- a/R/stat-boxplot.R +++ b/R/stat-boxplot.R @@ -27,7 +27,7 @@ StatBoxplot <- ggproto("StatBoxplot", Stat, default = NA) if (is.na(params$flipped_aes)) { - cli::cli_warn("Warning: Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'.") + cli::cli_warn("Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'.") params$flipped_aes <- FALSE } data <- flip_data(data, params$flipped_aes) diff --git a/tests/testthat/_snaps/geom-boxplot.md b/tests/testthat/_snaps/geom-boxplot.md index 213ae11e5a..8af5e6140b 100644 --- a/tests/testthat/_snaps/geom-boxplot.md +++ b/tests/testthat/_snaps/geom-boxplot.md @@ -1,14 +1,35 @@ # geom_boxplot for continuous x gives warning if more than one x (#992) - Warning: Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'. + Code + p <- bplot(aes(x, y)) + Condition + Warning: + Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'. + Warning: + Continuous x aesthetic + i did you forget `aes(group = ...)`? --- - Warning: Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'. + Code + p <- bplot(aes(x, y), facet_wrap(~x)) + Condition + Warning: + Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'. + Warning: + Continuous x aesthetic + i did you forget `aes(group = ...)`? --- - Warning: Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'. + Code + p <- bplot(aes(Sys.Date() + x, y)) + Condition + Warning: + Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'. + Warning: + Continuous x aesthetic + i did you forget `aes(group = ...)`? # boxplots with a group size >1 error diff --git a/tests/testthat/test-geom-boxplot.R b/tests/testthat/test-geom-boxplot.R index 9d977501ff..82c45c3fb7 100644 --- a/tests/testthat/test-geom-boxplot.R +++ b/tests/testthat/test-geom-boxplot.R @@ -42,9 +42,9 @@ test_that("geom_boxplot for continuous x gives warning if more than one x (#992) ggplot_build(ggplot(dat, aes) + geom_boxplot(aes) + extra) } - expect_snapshot_warning(bplot(aes(x, y))) - expect_snapshot_warning(bplot(aes(x, y), facet_wrap(~x))) - expect_snapshot_warning(bplot(aes(Sys.Date() + x, y))) + expect_snapshot(p <- bplot(aes(x, y))) + expect_snapshot(p <- bplot(aes(x, y), facet_wrap(~x))) + expect_snapshot(p <- bplot(aes(Sys.Date() + x, y))) expect_silent(bplot(aes(x, group = x, y))) expect_silent(bplot(aes(1, y))) From 180a2dbc10dad280577cf7f49bde752d8fd3b241 Mon Sep 17 00:00:00 2001 From: Sierra Johnson Date: Fri, 19 Sep 2025 10:09:28 -0600 Subject: [PATCH 3/3] code review change --- R/utilities.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utilities.R b/R/utilities.R index be98b47568..ca06985f26 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -562,7 +562,7 @@ has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA, } } - default + as.logical(default)[1] } #' @rdname bidirection #' @export