Skip to content
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
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)

* `coord_sf()` now has an argument `default_crs` that specifies the coordinate
reference system (CRS) for non-sf layers and scale/coord limits. This argument
defaults to the World Geodetic System 1984 (WGS84), which means x and y positions
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(names(data), ggplot_global$x_aes)
x <- if (length(x_aes) == 0) 0 else data[[x_aes[1]]]
y_aes <- intersect(names(data), ggplot_global$y_aes)
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)
}
)