Skip to content

Commit b7e11e1

Browse files
committed
adapt position_jitterdodge() for parity with position_dodge()
1 parent 6a447ba commit b7e11e1

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

R/position-jitterdodge.R

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#' geom_point(pch = 21, position = position_jitterdodge())
2222
position_jitterdodge <- function(jitter.width = NULL, jitter.height = 0,
2323
dodge.width = 0.75, reverse = FALSE,
24+
preserve = "total",
2425
seed = NA) {
2526
if (!is.null(seed) && is.na(seed)) {
2627
seed <- sample.int(.Machine$integer.max, 1L)
@@ -31,6 +32,7 @@ position_jitterdodge <- function(jitter.width = NULL, jitter.height = 0,
3132
jitter.width = jitter.width,
3233
jitter.height = jitter.height,
3334
dodge.width = dodge.width,
35+
preserve = arg_match0(preserve, c("total", "single")),
3436
reverse = reverse,
3537
seed = seed
3638
)
@@ -45,6 +47,8 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
4547
jitter.height = NULL,
4648
dodge.width = NULL,
4749
reverse = NULL,
50+
default_aes = aes(order = NULL),
51+
preserve = "total",
4852

4953
required_aes = c("x", "y"),
5054

@@ -53,27 +57,37 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
5357
data <- flip_data(data, flipped_aes)
5458
width <- self$jitter.width %||% (resolution(data$x, zero = FALSE, TRUE) * 0.4)
5559

56-
ndodge <- vec_unique(data[c("group", "PANEL", "x")])
57-
ndodge <- vec_group_id(ndodge[c("PANEL", "x")])
58-
ndodge <- max(tabulate(ndodge, attr(ndodge, "n")))
60+
if (identical(self$preserve, "total")) {
61+
n <- NULL
62+
} else {
63+
n <- vec_unique(data[c("group", "PANEL", "x")])
64+
n <- vec_group_id(n[c("PANEL", "x")])
65+
n <- max(tabulate(n, attr(n, "n")))
66+
}
5967

6068
list(
6169
dodge.width = self$dodge.width %||% 0.75,
6270
jitter.height = self$jitter.height %||% 0,
63-
jitter.width = width / (ndodge + 2),
71+
jitter.width = width / ((n %||% 1) + 2),
72+
n = n,
6473
seed = self$seed,
6574
flipped_aes = flipped_aes,
6675
reverse = self$reverse %||% FALSE
6776
)
6877
},
6978

79+
setup_data = function(self, data, params) {
80+
PositionDodge$setup_data(data = data, params = params)
81+
},
82+
7083
compute_panel = function(data, params, scales) {
7184
data <- flip_data(data, params$flipped_aes)
7285
data <- collide(
7386
data,
7487
params$dodge.width,
75-
"position_jitterdodge",
88+
name = "position_jitterdodge",
7689
strategy = pos_dodge,
90+
n = params$n,
7791
check.width = FALSE,
7892
reverse = !params$reverse # for consistency with `position_dodge2()`
7993
)

man/position_jitterdodge.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)