Skip to content

Commit

Permalink
add seed argument to position_jitterdodge (#2445)
Browse files Browse the repository at this point in the history
  • Loading branch information
slowkow authored and hadley committed Feb 12, 2018
1 parent 2b5b88d commit 39e4a3b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
5 changes: 3 additions & 2 deletions NEWS.md
Expand Up @@ -195,8 +195,9 @@ up correct aspect ratio, and draws a graticule.
default), or ensure that the width of a `single` element is preserved
(what many people want) (#1935).

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

* `stat_density()` has better behaviour if all groups are dropped because they
are too small (#2282).
Expand Down
22 changes: 13 additions & 9 deletions R/position-jitterdodge.R
Expand Up @@ -10,19 +10,24 @@
#' @param jitter.height degree of jitter in y direction. Defaults to 0.
#' @param dodge.width the amount to dodge in the x direction. Defaults to 0.75,
#' the default `position_dodge()` width.
#' @inheritParams position_jitter
#' @export
#' @examples
#' dsub <- diamonds[ sample(nrow(diamonds), 1000), ]
#' ggplot(dsub, aes(x = cut, y = carat, fill = clarity)) +
#' geom_boxplot(outlier.size = 0) +
#' geom_point(pch = 21, position = position_jitterdodge())
position_jitterdodge <- function(jitter.width = NULL, jitter.height = 0,
dodge.width = 0.75) {
dodge.width = 0.75, seed = NA) {
if (!is.null(seed) && is.na(seed)) {
seed <- sample.int(.Machine$integer.max, 1L)
}

ggproto(NULL, PositionJitterdodge,
jitter.width = jitter.width,
jitter.height = jitter.height,
dodge.width = dodge.width
dodge.width = dodge.width,
seed = seed
)
}

Expand Down Expand Up @@ -50,19 +55,18 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
list(
dodge.width = self$dodge.width,
jitter.height = self$jitter.height,
jitter.width = width / (ndodge + 2)
jitter.width = width / (ndodge + 2),
seed = self$seed
)
},


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

# then jitter
transform_position(data,
if (params$jitter.width > 0) function(x) jitter(x, amount = params$jitter.width),
if (params$jitter.height > 0) function(x) jitter(x, amount = params$jitter.height)
)
trans_x <- if (params$jitter.width > 0) function(x) jitter(x, amount = params$jitter.width)
trans_y <- if (params$jitter.height > 0) function(x) jitter(x, amount = params$jitter.height)

with_seed_null(params$seed, transform_position(data, trans_x, trans_y))
}
)
11 changes: 10 additions & 1 deletion man/position_jitterdodge.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 39e4a3b

Please sign in to comment.