Skip to content

Make sure jitter is only calculated once #4403

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

Merged
merged 4 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* Fix a bug in `position_jitter()` where different jitters would be applied to
different position aesthetics of the same axis (@thomasp85, #2941)

* `ggsave()` now uses ragg to render raster output if ragg is available
(@thomasp85, #4388)

Expand Down
13 changes: 12 additions & 1 deletion R/position-jitter.r
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ PositionJitter <- ggproto("PositionJitter", Position,
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)

with_seed_null(params$seed, transform_position(data, trans_x, trans_y))
# Make sure x and y jitter is only calculated once for all position aesthetics
x_aes <- intersect(ggplot_global$x_aes, names(data))
x <- if (length(x_aes) == 0) 0 else data[[x_aes[1]]]
y_aes <- intersect(ggplot_global$y_aes, names(data))
y <- if (length(y_aes) == 0) 0 else data[[y_aes[1]]]
dummy_data <- new_data_frame(list(x = x, y = y), nrow(data))
fixed_jitter <- with_seed_null(params$seed, transform_position(dummy_data, trans_x, trans_y))
x_jit <- fixed_jitter$x - x
y_jit <- fixed_jitter$y - y

# Apply jitter
transform_position(data, function(x) x + x_jit, function(x) x + y_jit)
}
)