Skip to content

Commit ba04941

Browse files
committed
Generalize position_jitterdodge to all possible dodge positions
Fixes #1494
1 parent f5c6f30 commit ba04941

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
* All `\donttest{}` examples run.
1111

12+
* `position_jitterdodge()` now works on all possible dodge aesthetics,
13+
e.g. `color`, `linetype` etc. instead of only based on `fill` (@bleutner)
14+
1215
* Removed a superfluous comma in `theme-defaults.r` code (@jschoeley)
1316

1417
* Fixed a compatibility issue with `ggproto` and R versions prior to 3.1.2.

R/position-jitterdodge.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,22 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
3535
jitter.height = NULL,
3636
dodge.width = NULL,
3737

38-
required_aes = c("x", "y", "fill"),
38+
required_aes = c("x", "y"),
3939

4040
setup_params = function(self, data) {
4141
width <- self$jitter.width %||% resolution(data$x, zero = FALSE) * 0.4
42-
# Adjust the x transformation based on the number of 'fill' variables
43-
nfill <- length(levels(data$fill))
44-
42+
# Adjust the x transformation based on the number of 'dodge' variables
43+
dodgecols <- intersect(c("fill", "colour", "linetype", "shape", "size", "alpha"), colnames(data))
44+
if (length(dodgecols) == 0) {
45+
stop("`position_jitterdodge()` requires at least one aesthetic to dodge by", call. = FALSE)
46+
}
47+
ndodge <- lapply(data[dodgecols], levels) # returns NULL for numeric, i.e. non-dodge layers
48+
ndodge <- length(unique(unlist(ndodge)))
49+
4550
list(
4651
dodge.width = self$dodge.width,
4752
jitter.height = self$jitter.height,
48-
jitter.width = width / (nfill + 2)
53+
jitter.width = width / (ndodge + 2)
4954
)
5055
},
5156

0 commit comments

Comments
 (0)