-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Aesthetics for position adjustments #6100
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
Changes from all commits
fe7abd1
b341653
f765685
e7de35e
c44ad91
93d5395
cef6def
442a6ac
4fb145f
6cbf5b9
32ad61b
46a9ab0
5fe2514
7ada465
ebf6fa7
4859dab
ef5b4ef
9e80c8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
#' @param reverse If `TRUE`, will reverse the default stacking order. | ||
#' This is useful if you're rotating both the plot and legend. | ||
#' @family position adjustments | ||
#' @eval rd_aesthetics("position", "dodge") | ||
#' | ||
#' @export | ||
#' @examples | ||
#' ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + | ||
|
@@ -104,7 +106,10 @@ PositionDodge <- ggproto("PositionDodge", Position, | |
preserve = "total", | ||
orientation = "x", | ||
reverse = NULL, | ||
default_aes = aes(order = NULL), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can be a bit worried that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is hard to search for, but I can confidently tell you that no package on CRAN has a |
||
|
||
setup_params = function(self, data) { | ||
|
||
flipped_aes <- has_flipped_aes(data, default = self$orientation == "y") | ||
check_required_aesthetics( | ||
if (flipped_aes) "y|ymin" else "x|xmin", | ||
|
@@ -139,9 +144,22 @@ PositionDodge <- ggproto("PositionDodge", Position, | |
|
||
setup_data = function(self, data, params) { | ||
data <- flip_data(data, params$flipped_aes) | ||
|
||
if (!"x" %in% names(data) && all(c("xmin", "xmax") %in% names(data))) { | ||
data$x <- (data$xmin + data$xmax) / 2 | ||
} | ||
|
||
data$order <- xtfrm( # xtfrm makes anything 'sortable' | ||
data$order %||% ave(data$group, data$x, data$PANEL, FUN = match_sorted) | ||
) | ||
if (params$reverse) { | ||
data$order <- -data$order | ||
} | ||
if (is.null(params$n)) { # preserve = "total" | ||
data$order <- ave(data$order, data$x, data$PANEL, FUN = match_sorted) | ||
} else { # preserve = "single" | ||
data$order <- match_sorted(data$order) | ||
} | ||
flip_data(data, params$flipped_aes) | ||
}, | ||
|
||
|
@@ -179,7 +197,7 @@ pos_dodge <- function(df, width, n = NULL) { | |
|
||
# Have a new group index from 1 to number of groups. | ||
# This might be needed if the group numbers in this set don't include all of 1:n | ||
groupidx <- match(df$group, unique0(df$group)) | ||
groupidx <- df$order %||% match_sorted(df$group) | ||
|
||
# Find the center for each group, then use that to calculate xmin and xmax | ||
df$x <- df$x + width * ((groupidx - 0.5) / n - 0.5) | ||
|
@@ -188,3 +206,7 @@ pos_dodge <- function(df, width, n = NULL) { | |
|
||
df | ||
} | ||
|
||
match_sorted <- function(x, y = x, ...) { | ||
vec_match(x, vec_sort(unique0(y), ...)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some restrictions here that don't apply to the classic aesthetics, right? You can't use
after_scale()
for instance. Is that somehow taken care of?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any surviving modifiers would be ignored.
after_stat()
is already evaluated, so that isn't really a concern.after_scale()
doesn't really apply as position adjustments are computed before scales are applied. It will be interpreted as there is no such aesthetic, therefore the default value is substituted.I'm not sure whether this was clear, but this does not replace
Geom$use_defaults()
, it just evaluates thePosition$default_aes
really.