Skip to content

Commit 0e72d95

Browse files
authored
Simplify scales$add_default (#5409)
1 parent 202029b commit 0e72d95

File tree

4 files changed

+11
-16
lines changed

4 files changed

+11
-16
lines changed

NEWS.md

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

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

R/aes-evaluation.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,10 @@ strip_stage <- function(expr) {
311311
if (is_call(uq_expr, c("after_stat", "after_scale"))) {
312312
uq_expr[[2]]
313313
} else if (is_call(uq_expr, "stage")) {
314+
uq_expr <- call_match(uq_expr, stage)
314315
# Prefer stat mapping if present, otherwise original mapping (fallback to
315316
# scale mapping) but there should always be two arguments to stage()
316-
uq_expr$after_stat %||% uq_expr$start %||% (if (is.null(uq_expr$after_scale)) uq_expr[[3]]) %||% uq_expr[[2]]
317+
uq_expr$after_stat %||% uq_expr$start %||% uq_expr$after_scale
317318
} else {
318319
expr
319320
}

R/layer.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,13 @@ Layer <- ggproto("Layer", NULL,
268268
aesthetics[["group"]] <- self$aes_params$group
269269
}
270270

271-
plot$scales$add_defaults(data, aesthetics, plot$plot_env)
272-
273271
# Evaluate aesthetics
274272
env <- child_env(baseenv(), stage = stage)
275273
evaled <- lapply(aesthetics, eval_tidy, data = data, env = env)
276274
evaled <- compact(evaled)
277275

276+
plot$scales$add_defaults(evaled, plot$plot_env)
277+
278278
# Check for discouraged usage in mapping
279279
warn_for_aes_extract_usage(aesthetics, data[setdiff(names(data), "PANEL")])
280280

@@ -376,7 +376,7 @@ Layer <- ggproto("Layer", NULL,
376376
stat_data <- data_frame0(!!!compact(stat_data))
377377

378378
# Add any new scales, if needed
379-
plot$scales$add_defaults(data, new, plot$plot_env)
379+
plot$scales$add_defaults(stat_data, plot$plot_env)
380380
# Transform the values, if the scale say it's ok
381381
# (see stat_spoke for one exception)
382382
if (self$stat$retransform) {

R/scales-.R

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,15 @@ ScalesList <- ggproto("ScalesList", NULL,
141141

142142
# `aesthetics` is a list of aesthetic-variable mappings. The name of each
143143
# item is the aesthetic, and the value of each item is the variable in data.
144-
add_defaults = function(self, data, aesthetics, env) {
145-
if (is.null(aesthetics)) {
146-
return()
147-
}
148-
names(aesthetics) <- unlist(lapply(names(aesthetics), aes_to_scale))
149-
150-
new_aesthetics <- setdiff(names(aesthetics), self$input())
144+
add_defaults = function(self, data, env) {
145+
new_aesthetics <- setdiff(names(data), self$input())
151146
# No new aesthetics, so no new scales to add
152147
if (is.null(new_aesthetics)) {
153148
return()
154149
}
155150

156-
data_cols <- lapply(aesthetics[new_aesthetics], eval_tidy, data = data)
157-
data_cols <- compact(data_cols)
158-
159-
for (aes in names(data_cols)) {
160-
self$add(find_scale(aes, data_cols[[aes]], env))
151+
for (aes in new_aesthetics) {
152+
self$add(find_scale(aes, data[[aes]], env))
161153
}
162154
},
163155

0 commit comments

Comments
 (0)