diff --git a/NEWS.md b/NEWS.md index f249de59e3..c3f58a65bc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ * The `name` argument in most scales is now explicitly the first argument (#5535) +* 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") )