From 9a564930ceefd1bc706c609982cf674121f2bfee Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 2 Apr 2024 11:34:37 +0200 Subject: [PATCH 1/3] Follow #4403 --- R/position-jitterdodge.R | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/R/position-jitterdodge.R b/R/position-jitterdodge.R index 291f03f263..fba28a47fa 100644 --- a/R/position-jitterdodge.R +++ b/R/position-jitterdodge.R @@ -76,7 +76,18 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position, 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) - data <- with_seed_null(params$seed, transform_position(data, trans_x, trans_y)) + x_aes <- intersect(ggplot_global$x_aes, names(data)) + y_aes <- intersect(ggplot_global$y_aes, names(data)) + + x <- if (length(x_aes) == 0) 0 else data[[x_aes[1]]] + y <- if (length(y_aes) == 0) 0 else data[[y_aes[1]]] + dummy_data <- data_frame0(x = x, y = y, .size = 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 + + data <- transform_position(data, function(x) x + x_jit, function(x) x + y_jit) flip_data(data, params$flipped_aes) } ) From f064deed1e78df5953e799a83529b0e8d2ce1dc0 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 2 Apr 2024 11:40:35 +0200 Subject: [PATCH 2/3] add test --- tests/testthat/test-position-jitterdodge.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-position-jitterdodge.R b/tests/testthat/test-position-jitterdodge.R index 8f6b9227eb..49e8378666 100644 --- a/tests/testthat/test-position-jitterdodge.R +++ b/tests/testthat/test-position-jitterdodge.R @@ -2,3 +2,15 @@ test_that("position_jitterdodge() fails with meaningful error", { p <- ggplot(mtcars) + geom_point(aes(disp, mpg), position = 'jitterdodge') expect_snapshot_error(ggplot_build(p)) }) + +test_that("position_jitterdodge preserves widths", { + ld <- layer_data( + ggplot(mtcars, aes(factor(cyl), fill = factor(am))) + + geom_bar(position = position_jitterdodge()) + ) + + expect_equal( + as.numeric(ld$xmax - ld$xmin), + rep(0.45, nrow(ld)) + ) +}) From c0f54e27572d3f8ec69268025c4b0d5110c35f34 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 2 Apr 2024 11:45:36 +0200 Subject: [PATCH 3/3] add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 5d24677959..08fb1da161 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,8 @@ # ggplot2 (development version) +* Fix a bug in `position_jitterdodge()` where different jitters would be applied + to different position aesthetics of the same axis (@teunbrand, #5818). * When facets coerce the faceting variables to factors, the 'ordered' class is dropped (@teunbrand, #5666). * `update_geom_defaults()` and `update_stat_defaults()` have a reset mechanism