From cd495df65c62a064abba1be94832d9a5824fa7fc Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 18 Dec 2025 10:23:45 +0100 Subject: [PATCH 1/3] count unique order entries per position, when order is provided --- R/position-dodge.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/position-dodge.R b/R/position-dodge.R index 9382bb2815..70d3419043 100644 --- a/R/position-dodge.R +++ b/R/position-dodge.R @@ -128,9 +128,10 @@ PositionDodge <- ggproto("PositionDodge", Position, n <- NULL } else { data$xmin <- data$xmin %||% data$x - cols <- intersect(colnames(data), c("group", "PANEL", "xmin")) + cols <- intersect(colnames(data), c("PANEL", "xmin")) + cols <- c(cols, if ("order" %in% names(data)) "order" else "group") n <- vec_unique(data[cols]) - n <- vec_group_id(n[setdiff(cols, "group")]) + n <- vec_group_id(n[setdiff(cols, c("group", "order"))]) n <- max(tabulate(n, attr(n, "n"))) } From 1d82be87af6d2a0d7e70cc6103e2ad1375cadbc5 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 18 Dec 2025 10:24:04 +0100 Subject: [PATCH 2/3] add test --- tests/testthat/test-position-dodge.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-position-dodge.R b/tests/testthat/test-position-dodge.R index 878ee6d155..36d2a92229 100644 --- a/tests/testthat/test-position-dodge.R +++ b/tests/testthat/test-position-dodge.R @@ -53,6 +53,18 @@ test_that("position_dodge() can use the order aesthetic", { expect_equal(ld$x, major + c(-0.2, 0, 0.2)[minor], ignore_attr = TRUE) }) +test_that("position_dodge() with duplicated order computes the correct width", { + df <- data_frame0( + group = c("A", "B", "C"), + order = c(1, 2, 2) + ) + ld <- layer_data( + ggplot(df, aes("X", 1, group = group, order = order)) + + geom_col(position = position_dodge(preserve = "single", width = 1), width = 1) + ) + expect_all_equal(ld$xmax - ld$xmin, 0.5) +}) + test_that("position_dodge warns about missing required aesthetics", { # Bit of a contrived geom to not have a required 'x' aesthetic From 3f7f3f2486f80e7b1c5a6f43df07005af947538a Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Thu, 18 Dec 2025 10:25:29 +0100 Subject: [PATCH 3/3] add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 8b14a183d6..556361f388 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* Fixed bug in `width` computation when `position_dodge(preserve = "single")` + had duplicated `order` aesthetic values (@teunbrand, #6775). * The `arrow` and `arrow.fill` arguments are now available in `geom_linerange()` and `geom_pointrange()` layers (@teunbrand, #6481). * (internal) `zeroGrob()` now returns a `grid::nullGrob()` (#6390).