Skip to content
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

Simplify scales$add_default #5409

Merged
merged 4 commits into from
Oct 2, 2023
Merged
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* `stage()` now works correctly, even with aesthetics that do not have scales
(#5408)
* `labeller()` now handles unspecified entries from lookup tables
(@92amartins, #4599).

Expand Down
3 changes: 2 additions & 1 deletion R/aes-evaluation.R
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,10 @@ strip_stage <- function(expr) {
if (is_call(uq_expr, c("after_stat", "after_scale"))) {
uq_expr[[2]]
} else if (is_call(uq_expr, "stage")) {
uq_expr <- call_match(uq_expr, stage)
# Prefer stat mapping if present, otherwise original mapping (fallback to
# scale mapping) but there should always be two arguments to stage()
uq_expr$after_stat %||% uq_expr$start %||% (if (is.null(uq_expr$after_scale)) uq_expr[[3]]) %||% uq_expr[[2]]
uq_expr$after_stat %||% uq_expr$start %||% uq_expr$after_scale
} else {
expr
}
Expand Down
6 changes: 3 additions & 3 deletions R/layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,13 @@ Layer <- ggproto("Layer", NULL,
aesthetics[["group"]] <- self$aes_params$group
}

plot$scales$add_defaults(data, aesthetics, plot$plot_env)

# Evaluate aesthetics
env <- child_env(baseenv(), stage = stage)
evaled <- lapply(aesthetics, eval_tidy, data = data, env = env)
evaled <- compact(evaled)

plot$scales$add_defaults(evaled, plot$plot_env)

# Check for discouraged usage in mapping
warn_for_aes_extract_usage(aesthetics, data[setdiff(names(data), "PANEL")])

Expand Down Expand Up @@ -376,7 +376,7 @@ Layer <- ggproto("Layer", NULL,
stat_data <- data_frame0(!!!compact(stat_data))

# Add any new scales, if needed
plot$scales$add_defaults(data, new, plot$plot_env)
plot$scales$add_defaults(stat_data, plot$plot_env)
# Transform the values, if the scale say it's ok
# (see stat_spoke for one exception)
if (self$stat$retransform) {
Expand Down
16 changes: 4 additions & 12 deletions R/scales-.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,15 @@ ScalesList <- ggproto("ScalesList", NULL,

# `aesthetics` is a list of aesthetic-variable mappings. The name of each
# item is the aesthetic, and the value of each item is the variable in data.
add_defaults = function(self, data, aesthetics, env) {
if (is.null(aesthetics)) {
return()
}
names(aesthetics) <- unlist(lapply(names(aesthetics), aes_to_scale))

new_aesthetics <- setdiff(names(aesthetics), self$input())
add_defaults = function(self, data, env) {
new_aesthetics <- setdiff(names(data), self$input())
# No new aesthetics, so no new scales to add
if (is.null(new_aesthetics)) {
return()
}

data_cols <- lapply(aesthetics[new_aesthetics], eval_tidy, data = data)
data_cols <- compact(data_cols)

for (aes in names(data_cols)) {
self$add(find_scale(aes, data_cols[[aes]], env))
for (aes in new_aesthetics) {
self$add(find_scale(aes, data[[aes]], env))
}
},

Expand Down
Loading