Skip to content

Commit 50a1c2f

Browse files
karawoohadley
authored andcommitted
Make position_dodge2 preserve total width by default (#2386)
* Make position_dodge2 preserve total width by default * Make sure preserve is set to "single" when varwidth == TRUE
1 parent 1d537ce commit 50a1c2f

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

R/geom-boxplot.r

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ geom_boxplot <- function(mapping = NULL, data = NULL,
111111
inherit.aes = TRUE) {
112112

113113
# varwidth = TRUE is not compatible with preserve = "total"
114-
if (!is.character(position)) {
114+
if (is.character(position)) {
115+
if (varwidth == TRUE) position <- position_dodge2(preserve = "single")
116+
} else {
115117
if (identical(position$preserve, "total") & varwidth == TRUE) {
116118
warning("Can't preserve total widths when varwidth = TRUE.", call. = FALSE)
117119
position$preserve <- "single"

R/position-dodge2.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ position_dodge2 <- function(width = NULL, preserve = c("single", "total"),
1919
#' @usage NULL
2020
#' @export
2121
PositionDodge2 <- ggproto("PositionDodge2", PositionDodge,
22-
preserve = "single",
22+
preserve = "total",
2323
padding = 0.1,
2424
reverse = FALSE,
2525

tests/testthat/test-position-dodge2.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,15 @@ test_that("padding argument controls space between elements", {
6464
expect_equal(gaps(d1), 0)
6565
expect_equal(gaps(d2), 0.0375)
6666
})
67+
68+
test_that("boxes in facetted plots keep the correct width", {
69+
70+
p <- ggplot(mtcars, aes(x = factor(vs), y = mpg)) +
71+
facet_wrap( ~ factor(cyl)) +
72+
geom_boxplot()
73+
74+
d <- layer_data(p)
75+
76+
expect_true(all(d$xmax - d$xmin == 0.75))
77+
78+
})

0 commit comments

Comments
 (0)