From 6f563009f6517a89edc4964d306e907c16bac702 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 9 Oct 2023 16:54:32 +0200 Subject: [PATCH 1/3] Include y-offset --- R/geom-dotplot.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/geom-dotplot.R b/R/geom-dotplot.R index 802a717c28..016bd031a7 100644 --- a/R/geom-dotplot.R +++ b/R/geom-dotplot.R @@ -242,12 +242,12 @@ GeomDotplot <- ggproto("GeomDotplot", Geom, # ymin, ymax, xmin, and xmax define the bounding rectangle for each stack # Can't do bounding box per dot, because y position isn't real. # After position code is rewritten, each dot should have its own bounding box. + yoffset <- if (is_mapped_discrete(data$y)) data$y else 0 data$xmin <- data$x - data$binwidth / 2 data$xmax <- data$x + data$binwidth / 2 - data$ymin <- stackaxismin - data$ymax <- stackaxismax - data$y <- 0 - + data$ymin <- stackaxismin + yoffset + data$ymax <- stackaxismax + yoffset + data$y <- yoffset } else if (params$binaxis == "y") { # ymin, ymax, xmin, and xmax define the bounding rectangle for each stack # Can't do bounding box per dot, because x position isn't real. From 19ab9a2f37e2986515193f7cbd6642b162839d83 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 9 Oct 2023 16:54:50 +0200 Subject: [PATCH 2/3] Add test --- ...bin-x-three-y-groups-stack-centerwhole.svg | 143 ++++++++++++++++++ tests/testthat/test-geom-dotplot.R | 3 + 2 files changed, 146 insertions(+) create mode 100644 tests/testthat/_snaps/geom-dotplot/bin-x-three-y-groups-stack-centerwhole.svg diff --git a/tests/testthat/_snaps/geom-dotplot/bin-x-three-y-groups-stack-centerwhole.svg b/tests/testthat/_snaps/geom-dotplot/bin-x-three-y-groups-stack-centerwhole.svg new file mode 100644 index 0000000000..7ac2842fc7 --- /dev/null +++ b/tests/testthat/_snaps/geom-dotplot/bin-x-three-y-groups-stack-centerwhole.svg @@ -0,0 +1,143 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +A +B +C + + + + + + + + +-2 +-1 +0 +1 +2 +y +x +bin x, three y groups, stack centerwhole + + diff --git a/tests/testthat/test-geom-dotplot.R b/tests/testthat/test-geom-dotplot.R index eedd4ba4e3..dc56e89125 100644 --- a/tests/testthat/test-geom-dotplot.R +++ b/tests/testthat/test-geom-dotplot.R @@ -154,6 +154,9 @@ test_that("geom_dotplot draws correctly", { # Binning along y, with multiple grouping factors dat2 <- data_frame(x = rep(factor(LETTERS[1:3]), 30), y = rnorm(90), g = rep(factor(LETTERS[1:2]), 45)) + expect_doppelganger("bin x, three y groups, stack centerwhole", + ggplot(dat2, aes(y, x)) + geom_dotplot(binwidth = .25, binaxis = "x", stackdir = "centerwhole") + ) expect_doppelganger("bin y, three x groups, stack centerwhole", ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = .25, binaxis = "y", stackdir = "centerwhole") ) From 2658f5e6fb3b64b17b84cfad6b56ea0deabf8a23 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 9 Oct 2023 16:55:01 +0200 Subject: [PATCH 3/3] Add news bullet --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index eb6f82c140..e19637e379 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* When using `geom_dotplot(binaxis = "x")` with a discrete y-variable, dots are + now stacked from the y-position rather than from 0 (@teunbrand, #5462) + * New function `check_device()` for testing the availability of advanced graphics features introduced in R 4.1.0 onwards (@teunbrand, #5332).