Skip to content

Generalize position_jitterdodge to all possible dodge positions #1494

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

Closed
Closed
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Bug fixes and minor improvements

* `position_jitterdodge()` now works on all possible dodge aesthetics,
e.g. `color`, `linetype` etc. instead of only based on `fill` (@bleutner)

* Removed a superfluous comma in `theme-defaults.r` code (@jschoeley)

* Fixed a compatibility issue with `ggproto` and R versions prior to 3.1.2.
Expand Down
15 changes: 10 additions & 5 deletions R/position-jitterdodge.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ PositionJitterdodge <- ggproto("PositionJitterdodge", Position,
jitter.height = NULL,
dodge.width = NULL,

required_aes = c("x", "y", "fill"),
required_aes = c("x", "y"),

setup_params = function(self, data) {
width <- self$jitter.width %||% resolution(data$x, zero = FALSE) * 0.4
# Adjust the x transformation based on the number of 'fill' variables
nfill <- length(levels(data$fill))

# Adjust the x transformation based on the number of 'dodge' variables
dodgecols <- intersect(c("fill", "colour", "linetype", "shape", "size", "alpha"), colnames(data))
if (length(dodgecols) == 0) {
stop("`position_jitterdodge()` requires at least one aesthetic to dodge by", call. = FALSE)
}
ndodge <- lapply(data[dodgecols], levels) # returns NULL for numeric, i.e. non-dodge layers
ndodge <- length(unique(unlist(ndodge)))

list(
dodge.width = self$dodge.width,
jitter.height = self$jitter.height,
jitter.width = width / (nfill + 2)
jitter.width = width / (ndodge + 2)
)
},

Expand Down