Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 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
77 changes: 77 additions & 0 deletions tests/testthat/_snaps/geom-dotplot/stack-center-stackratio-0-5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions tests/testthat/_snaps/geom-dotplot/stack-center-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.
Loading