Skip to content

Commit 7389477

Browse files
authored
Compute overlapping groups by panel (#2562)
In position_dodge and position_dodge2 Fixes #2493
1 parent 8e365a4 commit 7389477

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

R/position-dodge.r

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@
6767
#'
6868
#' ggplot(data = iris, aes(Species, Sepal.Length)) +
6969
#' geom_boxplot(aes(colour = Sepal.Width < 3.2), varwidth = TRUE)
70-
#'
70+
#'
7171
#' ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
7272
#' geom_bar(position = position_dodge2(preserve = "single"))
73-
#'
73+
#'
7474
#' ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
7575
#' geom_bar(position = position_dodge2(preserve = "total"))
7676
position_dodge <- function(width = NULL, preserve = c("total", "single")) {
@@ -96,7 +96,9 @@ PositionDodge <- ggproto("PositionDodge", Position,
9696
if (identical(self$preserve, "total")) {
9797
n <- NULL
9898
} else {
99-
n <- max(table(data$xmin))
99+
panels <- unname(split(data, data$PANEL))
100+
ns <- vapply(panels, function(panel) max(table(panel$xmin)), double(1))
101+
n <- max(ns)
100102
}
101103

102104
list(
@@ -111,7 +113,7 @@ PositionDodge <- ggproto("PositionDodge", Position,
111113
}
112114
data
113115
},
114-
116+
115117
compute_panel = function(data, params, scales) {
116118
collide(
117119
data,

R/position-dodge2.r

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,17 @@ PositionDodge2 <- ggproto("PositionDodge2", PositionDodge,
3131

3232
if (identical(self$preserve, "total")) {
3333
n <- NULL
34-
} else if ("x" %in% names(data)){
35-
n <- max(table(data$x))
3634
} else {
37-
n <- max(table(find_x_overlaps(data)))
35+
panels <- unname(split(data, data$PANEL))
36+
if ("x" %in% names(data)) {
37+
# Point geom
38+
groups <- lapply(panels, function(panel) table(panel$x))
39+
} else {
40+
# Interval geom
41+
groups <- lapply(panels, find_x_overlaps)
42+
}
43+
n_groups <- vapply(groups, max, double(1))
44+
n <- max(n_groups)
3845
}
3946

4047
list(

tests/testthat/test-position-dodge2.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,23 @@ test_that("boxes in facetted plots keep the correct width", {
8282
expect_true(all(d$xmax - d$xmin == 0.75))
8383

8484
})
85+
86+
test_that("width of groups computed per facet", {
87+
df <- tibble::tribble(
88+
~g1, ~g2, ~y,
89+
"x", "a", 1,
90+
"x", "b", 2,
91+
"y", "a", 3,
92+
"y", "b", 4,
93+
"y", "c", 3,
94+
)
95+
96+
p <- ggplot(df, aes("x", y, fill = g2)) +
97+
geom_col(position = position_dodge2(preserve = "single")) +
98+
facet_wrap(vars(g1))
99+
100+
d <- layer_data(p)
101+
width <- d$xmax - d$xmin
102+
103+
expect_true(all(width == (0.9 / 3) * 0.9))
104+
})

0 commit comments

Comments
 (0)