Skip to content

Commit 79b6e1c

Browse files
authored
Use group to dodge in position_jitterdodge() (#5922)
* dodge by max number of groups per panel/position * fill missing defaults * `position_jitterdodge()` doesn't fail this test-case anymore * add news bullet * Copy faster approach from #5928
1 parent 8f3a4ba commit 79b6e1c

File tree

4 files changed

+7
-26
lines changed

4 files changed

+7
-26
lines changed

NEWS.md

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

3+
* `position_jitterdodge()` now dodges by `group` (@teunbrand, #3656)
34
* The `arrow.fill` parameter is now applied to more line-based functions:
45
`geom_path()`, `geom_line()`, `geom_step()` `geom_function()`, line
56
geometries in `geom_sf()` and `element_line()`.

R/position-jitterdodge.R

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,14 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
4747
flipped_aes <- has_flipped_aes(data)
4848
data <- flip_data(data, flipped_aes)
4949
width <- self$jitter.width %||% (resolution(data$x, zero = FALSE, TRUE) * 0.4)
50-
# Adjust the x transformation based on the number of 'dodge' variables
51-
possible_dodge <- c("fill", "colour", "linetype", "shape", "size", "alpha")
52-
dodgecols <- intersect(possible_dodge, colnames(data))
53-
if (length(dodgecols) == 0) {
54-
cli::cli_abort(c(
55-
"{.fn position_jitterdodge} requires at least one aesthetic to dodge by.",
56-
i = "Use one of {.or {.val {possible_dodge}}} aesthetics."
57-
))
58-
}
59-
ndodge <- lapply(data[dodgecols], levels) # returns NULL for numeric, i.e. non-dodge layers
60-
ndodge <- vec_unique_count(unlist(ndodge))
50+
51+
ndodge <- vec_unique(data[c("group", "PANEL", "x")])
52+
ndodge <- vec_group_id(ndodge[c("PANEL", "x")])
53+
ndodge <- max(tabulate(ndodge, attr(ndodge, "n")))
6154

6255
list(
63-
dodge.width = self$dodge.width,
64-
jitter.height = self$jitter.height,
56+
dodge.width = self$dodge.width %||% 0.75,
57+
jitter.height = self$jitter.height %||% 0,
6558
jitter.width = width / (ndodge + 2),
6659
seed = self$seed,
6760
flipped_aes = flipped_aes

tests/testthat/_snaps/position-jitterdodge.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/testthat/test-position-jitterdodge.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
test_that("position_jitterdodge() fails with meaningful error", {
2-
p <- ggplot(mtcars) + geom_point(aes(disp, mpg), position = 'jitterdodge')
3-
expect_snapshot_error(ggplot_build(p))
4-
})
5-
61
test_that("position_jitterdodge preserves widths", {
72
ld <- layer_data(
83
ggplot(mtcars, aes(factor(cyl), fill = factor(am))) +

0 commit comments

Comments
 (0)