Skip to content

Commit f1ba983

Browse files
committed
Merge branch master into label-tag
1 parent 7859a29 commit f1ba983

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ up correct aspect ratio, and draws a graticule.
200200
default), or ensure that the width of a `single` element is preserved
201201
(what many people want) (#1935).
202202

203-
* `position_jitter()` gains a `seed` argument that allows specifying a random
204-
seed for reproducible jittering (#1996, @krlmlr).
203+
* `position_jitter()` and `position_jitterdodge()` gain a `seed` argument that
204+
allows specifying a random seed for reproducible jittering (@krlmlr, #1996
205+
and @slowkow, #2445).
205206

206207
* `stat_density()` has better behaviour if all groups are dropped because they
207208
are too small (#2282).

R/position-jitterdodge.R

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@
1010
#' @param jitter.height degree of jitter in y direction. Defaults to 0.
1111
#' @param dodge.width the amount to dodge in the x direction. Defaults to 0.75,
1212
#' the default `position_dodge()` width.
13+
#' @inheritParams position_jitter
1314
#' @export
1415
#' @examples
1516
#' dsub <- diamonds[ sample(nrow(diamonds), 1000), ]
1617
#' ggplot(dsub, aes(x = cut, y = carat, fill = clarity)) +
1718
#' geom_boxplot(outlier.size = 0) +
1819
#' geom_point(pch = 21, position = position_jitterdodge())
1920
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+
}
2125

2226
ggproto(NULL, PositionJitterdodge,
2327
jitter.width = jitter.width,
2428
jitter.height = jitter.height,
25-
dodge.width = dodge.width
29+
dodge.width = dodge.width,
30+
seed = seed
2631
)
2732
}
2833

@@ -50,19 +55,18 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
5055
list(
5156
dodge.width = self$dodge.width,
5257
jitter.height = self$jitter.height,
53-
jitter.width = width / (ndodge + 2)
58+
jitter.width = width / (ndodge + 2),
59+
seed = self$seed
5460
)
5561
},
5662

57-
5863
compute_panel = function(data, params, scales) {
5964
data <- collide(data, params$dodge.width, "position_jitterdodge", pos_dodge,
6065
check.width = FALSE)
6166

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))
6771
}
6872
)

man/position_jitterdodge.Rd

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)