Skip to content

Fix misalignment in geom_dotplot when stackratio != 1 and stackdir != "up" #4734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
* Updated documentation for `geom_contour()` to correctly reflect argument
precedence between `bins` and `binwidth`. (@eliocamp, #4651)

* Dots in `geom_dotplot()` are now correctly aligned to the baseline when
`stackratio != 1` and `stackdir != "up"` (@mjskay, #4614)

# ggplot2 3.3.5
This is a very small release focusing on fixing a couple of untenable issues
Expand Down
2 changes: 1 addition & 1 deletion R/geom-dotplot.r
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,

ggname("geom_dotplot",
dotstackGrob(stackaxis = stackaxis, x = tdata$x, y = tdata$y, dotdia = dotdianpc,
stackposition = tdata$stackpos, stackratio = stackratio,
stackposition = tdata$stackpos, stackdir = stackdir, stackratio = stackratio,
default.units = "npc",
gp = gpar(col = alpha(tdata$colour, tdata$alpha),
fill = alpha(tdata$fill, tdata$alpha),
Expand Down
20 changes: 17 additions & 3 deletions R/grob-dotstack.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dotstackGrob <- function(
stackaxis = "y",
dotdia = unit(1, "npc"), # Dot diameter in the non-stack axis, should be in npc
stackposition = 0, # Position of each dot in the stack, relative to origin
stackdir = "up", # Stacking direction ("up", "down", "center", or "centerwhole")
stackratio = 1, # Stacking height of dots (.75 means 25% dot overlap)
default.units = "npc", name = NULL, gp = gpar(), vp = NULL)
{
Expand All @@ -17,7 +18,7 @@ dotstackGrob <- function(
warn("Unit type of dotdia should be 'npc'")

grob(x = x, y = y, stackaxis = stackaxis, dotdia = dotdia,
stackposition = stackposition, stackratio = stackratio,
stackposition = stackposition, stackdir = stackdir, stackratio = stackratio,
name = name, gp = gp, vp = vp, cl = "dotstackGrob")
}
# Only cross-version reliable way to check the unit of a unit object
Expand All @@ -31,14 +32,27 @@ makeContext.dotstackGrob <- function(x, recording = TRUE) {
xmm <- convertX(x$x, "mm", valueOnly = TRUE)
ymm <- convertY(x$y, "mm", valueOnly = TRUE)

# When stacking up (or down), stackratios != 1 will cause the bottom (top)
# edge of the first dot in a stack to no longer touch the origin, as
# stackpositions are expanded or contracted away from the dotstack's origin.
# The stackoffset corrects that misalignment so that the first dot just
# touches the dotstack's origin.
if (is.null(x$stackdir) || x$stackdir == "up") {
stackoffset <- (1 - x$stackratio) / 2
} else if (x$stackdir == "down") {
stackoffset <- -(1 - x$stackratio) / 2
} else {
stackoffset <- 0
}

if (x$stackaxis == "x") {
dotdiamm <- convertY(x$dotdia, "mm", valueOnly = TRUE)
xpos <- xmm + dotdiamm * (x$stackposition * x$stackratio + (1 - x$stackratio) / 2)
xpos <- xmm + dotdiamm * (x$stackposition * x$stackratio + stackoffset)
ypos <- ymm
} else if (x$stackaxis == "y") {
dotdiamm <- convertX(x$dotdia, "mm", valueOnly = TRUE)
xpos <- xmm
ypos <- ymm + dotdiamm * (x$stackposition * x$stackratio + (1 - x$stackratio) / 2)
ypos <- ymm + dotdiamm * (x$stackposition * x$stackratio + stackoffset)
}

circleGrob(
Expand Down
74 changes: 74 additions & 0 deletions tests/testthat/_snaps/geom-dotplot/stackratio-1-5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions tests/testthat/test-geom-dotplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,16 @@ test_that("geom_dotplot draws correctly", {
ggplot(dat2, aes(0, x)) + geom_dotplot(binwidth = .4, binaxis = "y", stackdir = "center")
))
})

test_that("stackratio != 1 works", {
df <- data.frame(x = c(rep(1, 3), rep(2, 2)))

expect_doppelganger("stackratio = 1.5",
ggplot(df) +
geom_hline(yintercept = 0) +
geom_dotplot(aes(x), binwidth = 0.5, stackdir = "down", stackratio = 1.5, fill = NA) +
geom_dotplot(aes(x + 3), binwidth = 0.5, stackdir = "up", stackratio = 1.5, fill = NA) +
geom_dotplot(aes(x + 6), binwidth = 0.5, stackdir = "center", stackratio = 1.5, fill = NA) +
geom_dotplot(aes(x + 9), binwidth = 0.5, stackdir = "centerwhole", stackratio = 1.5, fill = NA)
)
})