Skip to content

Commit 668a51e

Browse files
authored
Ensure correct positioning of dots in geom_dotplot (#4417)
1 parent d26b19c commit 668a51e

File tree

5 files changed

+67
-59
lines changed

5 files changed

+67
-59
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+
* Fix bug in `geom_dotplot()` where dots would be positioned wrong with
4+
`stackgroups = TRUE` (@thomasp85, #1745)
5+
36
* Make sure position_jitter creates the same jittering independent of whether it
47
is called by name or with constructor (@thomasp85, #2507)
58

R/geom-dotplot.r

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ geom_dotplot <- function(mapping = NULL, data = NULL,
149149
if (stackgroups && method == "dotdensity" && binpositions == "bygroup")
150150
message('geom_dotplot called with stackgroups=TRUE and method="dotdensity". You probably want to set binpositions="all"')
151151

152+
stackdir <- arg_match0(stackdir, c("up", "down", "center", "centerwhole"), "stackdir")
152153
layer(
153154
data = data,
154155
mapping = mapping,
@@ -210,23 +211,27 @@ GeomDotplot <- ggproto("GeomDotplot", Geom,
210211
stackaxismax <- .5
211212
}
212213

213-
214214
# Fill the bins: at a given x (or y), if count=3, make 3 entries at that x
215215
data <- data[rep(1:nrow(data), data$count), ]
216216

217217
# Next part will set the position of each dot within each stack
218218
# If stackgroups=TRUE, split only on x (or y) and panel; if not stacking, also split by group
219219
plyvars <- params$binaxis %||% "x"
220+
stackaxis <- setdiff(c("x", "y"), plyvars)
220221
plyvars <- c(plyvars, "PANEL")
221222
if (is.null(params$stackgroups) || !params$stackgroups)
222223
plyvars <- c(plyvars, "group")
223224

225+
if (stackaxis == "x") {
226+
plyvars <- c(plyvars, "x")
227+
}
228+
224229
# Within each x, or x+group, set countidx=1,2,3, and set stackpos according to stack function
225230
data <- dapply(data, plyvars, function(xx) {
226-
xx$countidx <- 1:nrow(xx)
227-
xx$stackpos <- stackdots(xx$countidx)
228-
xx
229-
})
231+
xx$countidx <- 1:nrow(xx)
232+
xx$stackpos <- stackdots(xx$countidx)
233+
xx
234+
})
230235

231236

232237
# Set the bounding boxes for the dots

tests/figs/deps.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
- vdiffr-svg-engine: 1.0
22
- vdiffr: 0.3.3
3-
- freetypeharfbuzz: 0.2.5
3+
- freetypeharfbuzz: 0.2.6

0 commit comments

Comments
 (0)