Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproducible jitter #1996

Merged
merged 20 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions R/position-jitter.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#' jitter values will occupy 80\% of the implied bins. Categorical data
#' is aligned on the integers, so a width or height of 0.5 will spread the
#' data so it's not possible to see the distinction between the categories.
#' @param seed An optional random seed to make the jitter reproducible.
#' Useful if you need to apply the same jitter twice, e.g., for a point and
#' a corresponding label.
#' @export
#' @examples
#' # Jittering is useful when you have a discrete position, and a relatively
Expand All @@ -31,10 +34,11 @@
#' geom_jitter(width = 0.1, height = 0.1)
#' ggplot(mtcars, aes(am, vs)) +
#' geom_jitter(position = position_jitter(width = 0.1, height = 0.1))
position_jitter <- function(width = NULL, height = NULL) {
position_jitter <- function(width = NULL, height = NULL, seed = NULL) {
ggproto(NULL, PositionJitter,
width = width,
height = height
height = height,
seed = seed
)
}

Expand All @@ -48,14 +52,15 @@ PositionJitter <- ggproto("PositionJitter", Position,
setup_params = function(self, data) {
list(
width = self$width %||% (resolution(data$x, zero = FALSE) * 0.4),
height = self$height %||% (resolution(data$y, zero = FALSE) * 0.4)
height = self$height %||% (resolution(data$y, zero = FALSE) * 0.4),
seed = self$seed
)
},

compute_layer = function(data, params, panel) {
trans_x <- if (params$width > 0) function(x) jitter(x, amount = params$width)
trans_y <- if (params$height > 0) function(x) jitter(x, amount = params$height)

transform_position(data, trans_x, trans_y)
with_seed(params$seed, transform_position(data, trans_x, trans_y))
}
)
10 changes: 10 additions & 0 deletions R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ find_args <- function(...) {
# global data
dummy_data <- function() data.frame(x = NA)

with_seed <- function(seed, code) {
if (!is.null(seed)) {
old_seed <- get0(".Random.seed", globalenv())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this definitely ok to do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yihui: knitr seems to cache/restore .Random.seed, too. The caching in knitr is explicit, but it looks like the restore happens implicitly. Could you please comment if my way of restoring the random seed is safe?

with_seed <- function(seed, code) {
  if (!is.null(seed)) {
    old_seed <- get0(".Random.seed", globalenv())
    if (!is.null(old_seed)) {
      on.exit(assign(".Random.seed", old_seed, globalenv()), add = TRUE)
    }
  }
  code
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is safe (I'd add mode = 'integer' to get0(), though), but it seems that you forgot to set.seed(seed) before code is evaluated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. The code worked by accident because resetting the random seed after applying the jitter also made it reproducible.

if (!is.null(old_seed)) {
on.exit(assign(".Random.seed", old_seed, globalenv()), add = TRUE)
}
}
code
}

# Needed to trigger package loading
#' @importFrom tibble tibble
NULL
4 changes: 3 additions & 1 deletion man/position_jitter.Rd

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