From 4d7e202d1345f702814220c19d3e9fb5669b849d Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Thu, 14 Dec 2023 15:13:34 +0100 Subject: [PATCH] Dotplots with discrete `y` (#5463) * Include y-offset * Add test * Add news bullet --- NEWS.md | 3 + R/geom-dotplot.R | 8 +- ...bin-x-three-y-groups-stack-centerwhole.svg | 143 ++++++++++++++++++ tests/testthat/test-geom-dotplot.R | 3 + 4 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/_snaps/geom-dotplot/bin-x-three-y-groups-stack-centerwhole.svg diff --git a/NEWS.md b/NEWS.md index cfc08264c2..942991d9dd 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) + * (breaking) In the `scale_{colour/fill}_gradient2()` and `scale_{colour/fill}_steps2()` functions, the `midpoint` argument is transformed by the scale transformation (#3198). diff --git a/R/geom-dotplot.R b/R/geom-dotplot.R index 120fb80109..6114f2841e 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. 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 69b7d65a75..8f9aae6024 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") )