Skip to content

Commit

Permalink
Dotplots with discrete y (#5463)
Browse files Browse the repository at this point in the history
* Include y-offset

* Add test

* Add news bullet
  • Loading branch information
teunbrand authored Dec 14, 2023
1 parent a417cf4 commit 4d7e202
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
8 changes: 4 additions & 4 deletions R/geom-dotplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions tests/testthat/test-geom-dotplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down

0 comments on commit 4d7e202

Please sign in to comment.