|
32 | 32 | #' Below follows an overview of the three stages of evaluation and how aesthetic
|
33 | 33 | #' evaluation can be controlled.
|
34 | 34 | #'
|
35 |
| -#' ## Stage 1: direct input |
| 35 | +#' ## Stage 1: direct input at the start |
36 | 36 | #' The default is to map at the beginning, using the layer data provided by
|
37 | 37 | #' the user. If you want to map directly from the layer data you should not do
|
38 | 38 | #' anything special. This is the only stage where the original layer data can
|
|
87 | 87 | #' ```
|
88 | 88 | #'
|
89 | 89 | #' ## 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. |
93 | 95 | #'
|
94 | 96 | #' ```r
|
95 | 97 | #' # Use stage to modify the scaled fill
|
96 | 98 | #' ggplot(mpg, aes(class, hwy)) +
|
97 | 99 | #' geom_boxplot(aes(fill = stage(class, after_scale = alpha(fill, 0.4))))
|
98 | 100 | #'
|
99 | 101 | #' # 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. |
101 | 103 | #' ggplot(mpg, aes(class, displ)) +
|
102 | 104 | #' geom_violin() +
|
103 | 105 | #' stat_summary(
|
|
110 | 112 | #' )
|
111 | 113 | #' ```
|
112 | 114 | #'
|
| 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 | +#' |
113 | 120 | #' ## Theme access
|
114 | 121 | #' The `from_theme()` function can be used to acces the [`element_geom()`]
|
115 | 122 | #' fields of the `theme(geom)` argument. Using `aes(colour = from_theme(ink))`
|
@@ -332,7 +339,7 @@ strip_stage <- function(expr) {
|
332 | 339 | } else if (is_call(uq_expr, "stage")) {
|
333 | 340 | uq_expr <- call_match(uq_expr, stage)
|
334 | 341 | # 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) |
336 | 343 | uq_expr$after_stat %||% uq_expr$start %||% uq_expr$after_scale
|
337 | 344 | } else {
|
338 | 345 | expr
|
|
0 commit comments