Skip to content

Commit 4d7e202

Browse files
authored
Dotplots with discrete y (#5463)
* Include y-offset * Add test * Add news bullet
1 parent a417cf4 commit 4d7e202

File tree

4 files changed

+153
-4
lines changed

4 files changed

+153
-4
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* When using `geom_dotplot(binaxis = "x")` with a discrete y-variable, dots are
4+
now stacked from the y-position rather than from 0 (@teunbrand, #5462)
5+
36
* (breaking) In the `scale_{colour/fill}_gradient2()` and
47
`scale_{colour/fill}_steps2()` functions, the `midpoint` argument is
58
transformed by the scale transformation (#3198).

R/geom-dotplot.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,12 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
242242
# ymin, ymax, xmin, and xmax define the bounding rectangle for each stack
243243
# Can't do bounding box per dot, because y position isn't real.
244244
# After position code is rewritten, each dot should have its own bounding box.
245+
yoffset <- if (is_mapped_discrete(data$y)) data$y else 0
245246
data$xmin <- data$x - data$binwidth / 2
246247
data$xmax <- data$x + data$binwidth / 2
247-
data$ymin <- stackaxismin
248-
data$ymax <- stackaxismax
249-
data$y <- 0
250-
248+
data$ymin <- stackaxismin + yoffset
249+
data$ymax <- stackaxismax + yoffset
250+
data$y <- yoffset
251251
} else if (params$binaxis == "y") {
252252
# ymin, ymax, xmin, and xmax define the bounding rectangle for each stack
253253
# Can't do bounding box per dot, because x position isn't real.
Lines changed: 143 additions & 0 deletions
Loading

tests/testthat/test-geom-dotplot.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ test_that("geom_dotplot draws correctly", {
154154
# Binning along y, with multiple grouping factors
155155
dat2 <- data_frame(x = rep(factor(LETTERS[1:3]), 30), y = rnorm(90), g = rep(factor(LETTERS[1:2]), 45))
156156

157+
expect_doppelganger("bin x, three y groups, stack centerwhole",
158+
ggplot(dat2, aes(y, x)) + geom_dotplot(binwidth = .25, binaxis = "x", stackdir = "centerwhole")
159+
)
157160
expect_doppelganger("bin y, three x groups, stack centerwhole",
158161
ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = .25, binaxis = "y", stackdir = "centerwhole")
159162
)

0 commit comments

Comments
 (0)