Skip to content

Commit ddd207e

Browse files
authored
Add test for using stage() with a mapping specified for start only (#6131)
* add test for using stage with a mapping to start only * clean up outdated comment * add to documentation
1 parent ad97679 commit ddd207e

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

R/aes-evaluation.R

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#' Below follows an overview of the three stages of evaluation and how aesthetic
3333
#' evaluation can be controlled.
3434
#'
35-
#' ## Stage 1: direct input
35+
#' ## Stage 1: direct input at the start
3636
#' The default is to map at the beginning, using the layer data provided by
3737
#' the user. If you want to map directly from the layer data you should not do
3838
#' anything special. This is the only stage where the original layer data can
@@ -87,17 +87,19 @@
8787
#' ```
8888
#'
8989
#' ## Complex staging
90-
#' If you want to map the same aesthetic multiple times, e.g. map `x` to a
91-
#' data column for the stat, but remap it for the geom, you can use the
92-
#' `stage()` function to collect multiple mappings.
90+
#' Sometimes, you may want to map the same aesthetic multiple times, e.g. map
91+
#' `x` to a data column at the start for the layer stat, but remap it later to
92+
#' a variable from the stat transformation for the layer geom. The `stage()`
93+
#' function allows you to control multiple mappings for the same aesthetic
94+
#' across all three stages of evaluation.
9395
#'
9496
#' ```r
9597
#' # Use stage to modify the scaled fill
9698
#' ggplot(mpg, aes(class, hwy)) +
9799
#' geom_boxplot(aes(fill = stage(class, after_scale = alpha(fill, 0.4))))
98100
#'
99101
#' # Using data for computing summary, but placing label elsewhere.
100-
#' # Also, we're making our own computed variable to use for the label.
102+
#' # Also, we're making our own computed variables to use for the label.
101103
#' ggplot(mpg, aes(class, displ)) +
102104
#' geom_violin() +
103105
#' stat_summary(
@@ -110,6 +112,11 @@
110112
#' )
111113
#' ```
112114
#'
115+
#' Conceptually, `aes(x)` is equivalent to `aes(stage(start = x))`, and
116+
#' `aes(after_stat(count))` is equivalent to `aes(stage(after_stat = count))`,
117+
#' and so on. `stage()` is most useful when at least two of its arguments are
118+
#' specified.
119+
#'
113120
#' ## Theme access
114121
#' The `from_theme()` function can be used to acces the [`element_geom()`]
115122
#' fields of the `theme(geom)` argument. Using `aes(colour = from_theme(ink))`
@@ -332,7 +339,7 @@ strip_stage <- function(expr) {
332339
} else if (is_call(uq_expr, "stage")) {
333340
uq_expr <- call_match(uq_expr, stage)
334341
# Prefer stat mapping if present, otherwise original mapping (fallback to
335-
# scale mapping) but there should always be two arguments to stage()
342+
# scale mapping)
336343
uq_expr$after_stat %||% uq_expr$start %||% uq_expr$after_scale
337344
} else {
338345
expr

man/aes_eval.Rd

Lines changed: 12 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-aes-calculated.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,27 @@ test_that("functions can be masked", {
124124
expect_equal(evaled, list(x = 10, y = 30))
125125

126126
})
127+
128+
test_that("stage allows aesthetics that are only mapped to start", {
129+
130+
df <- data.frame(x = 1:2)
131+
132+
start_unnamed <- aes(stage(x))
133+
expect_equal(
134+
eval_aesthetics(start_unnamed, data = df),
135+
list(x = 1:2)
136+
)
137+
138+
start_named <- aes(stage(start = x))
139+
expect_equal(
140+
eval_aesthetics(start_named, data = df),
141+
list(x = 1:2)
142+
)
143+
144+
start_nulls <- aes(stage(start = x, after_stat = NULL, after_scale = NULL))
145+
expect_equal(
146+
eval_aesthetics(start_nulls, data = df),
147+
list(x = 1:2)
148+
)
149+
150+
})

0 commit comments

Comments
 (0)