|
10 | 10 | #' @param jitter.height degree of jitter in y direction. Defaults to 0.
|
11 | 11 | #' @param dodge.width the amount to dodge in the x direction. Defaults to 0.75,
|
12 | 12 | #' the default `position_dodge()` width.
|
| 13 | +#' @inheritParams position_jitter |
13 | 14 | #' @export
|
14 | 15 | #' @examples
|
15 | 16 | #' dsub <- diamonds[ sample(nrow(diamonds), 1000), ]
|
16 | 17 | #' ggplot(dsub, aes(x = cut, y = carat, fill = clarity)) +
|
17 | 18 | #' geom_boxplot(outlier.size = 0) +
|
18 | 19 | #' geom_point(pch = 21, position = position_jitterdodge())
|
19 | 20 | position_jitterdodge <- function(jitter.width = NULL, jitter.height = 0,
|
20 |
| - dodge.width = 0.75) { |
| 21 | + dodge.width = 0.75, seed = NA) { |
| 22 | + if (!is.null(seed) && is.na(seed)) { |
| 23 | + seed <- sample.int(.Machine$integer.max, 1L) |
| 24 | + } |
21 | 25 |
|
22 | 26 | ggproto(NULL, PositionJitterdodge,
|
23 | 27 | jitter.width = jitter.width,
|
24 | 28 | jitter.height = jitter.height,
|
25 |
| - dodge.width = dodge.width |
| 29 | + dodge.width = dodge.width, |
| 30 | + seed = seed |
26 | 31 | )
|
27 | 32 | }
|
28 | 33 |
|
@@ -50,19 +55,18 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
|
50 | 55 | list(
|
51 | 56 | dodge.width = self$dodge.width,
|
52 | 57 | jitter.height = self$jitter.height,
|
53 |
| - jitter.width = width / (ndodge + 2) |
| 58 | + jitter.width = width / (ndodge + 2), |
| 59 | + seed = self$seed |
54 | 60 | )
|
55 | 61 | },
|
56 | 62 |
|
57 |
| - |
58 | 63 | compute_panel = function(data, params, scales) {
|
59 | 64 | data <- collide(data, params$dodge.width, "position_jitterdodge", pos_dodge,
|
60 | 65 | check.width = FALSE)
|
61 | 66 |
|
62 |
| - # then jitter |
63 |
| - transform_position(data, |
64 |
| - if (params$jitter.width > 0) function(x) jitter(x, amount = params$jitter.width), |
65 |
| - if (params$jitter.height > 0) function(x) jitter(x, amount = params$jitter.height) |
66 |
| - ) |
| 67 | + trans_x <- if (params$jitter.width > 0) function(x) jitter(x, amount = params$jitter.width) |
| 68 | + trans_y <- if (params$jitter.height > 0) function(x) jitter(x, amount = params$jitter.height) |
| 69 | + |
| 70 | + with_seed_null(params$seed, transform_position(data, trans_x, trans_y)) |
67 | 71 | }
|
68 | 72 | )
|
0 commit comments