Skip to content

Commit 57414df

Browse files
Add warning when boxplot orientation is ambiguous fixes #4250 (#6631)
1 parent 5beed01 commit 57414df

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

R/stat-boxplot.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ StatBoxplot <- ggproto("StatBoxplot", Stat,
2323
setup_params = function(self, data, params) {
2424
params$flipped_aes <- has_flipped_aes(data, params, main_is_orthogonal = TRUE,
2525
group_has_equal = TRUE,
26-
main_is_optional = TRUE)
26+
main_is_optional = TRUE,
27+
default = NA)
28+
29+
if (is.na(params$flipped_aes)) {
30+
cli::cli_warn("Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'.")
31+
params$flipped_aes <- FALSE
32+
}
2733
data <- flip_data(data, params$flipped_aes)
2834

2935
has_x <- !(is.null(data$x) && is.null(params$x))

R/utilities.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ has_flipped_aes <- function(data, params = list(), main_is_orthogonal = NA,
562562
}
563563
}
564564

565-
isTRUE(default)
565+
as.logical(default)[1]
566566
}
567567
#' @rdname bidirection
568568
#' @export

tests/testthat/_snaps/geom-boxplot.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
# geom_boxplot for continuous x gives warning if more than one x (#992)
22

3-
Continuous x aesthetic
4-
i did you forget `aes(group = ...)`?
3+
Code
4+
p <- bplot(aes(x, y))
5+
Condition
6+
Warning:
7+
Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'.
8+
Warning:
9+
Continuous x aesthetic
10+
i did you forget `aes(group = ...)`?
511

612
---
713

8-
Continuous x aesthetic
9-
i did you forget `aes(group = ...)`?
14+
Code
15+
p <- bplot(aes(x, y), facet_wrap(~x))
16+
Condition
17+
Warning:
18+
Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'.
19+
Warning:
20+
Continuous x aesthetic
21+
i did you forget `aes(group = ...)`?
1022

1123
---
1224

13-
Continuous x aesthetic
14-
i did you forget `aes(group = ...)`?
25+
Code
26+
p <- bplot(aes(Sys.Date() + x, y))
27+
Condition
28+
Warning:
29+
Orientation is not uniquely specified when both the x and y aesthetics are continuous. Picking default orientation 'x'.
30+
Warning:
31+
Continuous x aesthetic
32+
i did you forget `aes(group = ...)`?
1533

1634
# boxplots with a group size >1 error
1735

tests/testthat/test-geom-boxplot.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ test_that("geom_boxplot for continuous x gives warning if more than one x (#992)
4242
ggplot_build(ggplot(dat, aes) + geom_boxplot(aes) + extra)
4343
}
4444

45-
expect_snapshot_warning(bplot(aes(x, y)))
46-
expect_snapshot_warning(bplot(aes(x, y), facet_wrap(~x)))
47-
expect_snapshot_warning(bplot(aes(Sys.Date() + x, y)))
45+
expect_snapshot(p <- bplot(aes(x, y)))
46+
expect_snapshot(p <- bplot(aes(x, y), facet_wrap(~x)))
47+
expect_snapshot(p <- bplot(aes(Sys.Date() + x, y)))
4848

4949
expect_silent(bplot(aes(x, group = x, y)))
5050
expect_silent(bplot(aes(1, y)))

0 commit comments

Comments
 (0)