|
6 | 6 | #' three functions to control at which stage aesthetics should be evaluated.
|
7 | 7 | #'
|
8 | 8 | #' @usage # These functions can be used inside the `aes()` function
|
9 |
| -#' # used as the `mapping` argument in layers. |
| 9 | +#' # used as the `mapping` argument in layers, for example: |
| 10 | +#' # geom_density(mapping = aes(y = after_stat(scaled))) |
10 | 11 | #'
|
11 | 12 | #' @param x <[`data-masking`][rlang::topic-data-mask]> An aesthetic expression
|
12 | 13 | #' using variables calculated by the stat (`after_stat()`) or layer aesthetics
|
|
18 | 19 | #' @param after_scale <[`data-masking`][rlang::topic-data-mask]> An aesthetic
|
19 | 20 | #' expression using layer aesthetics.
|
20 | 21 | #'
|
21 |
| -#' @note |
22 |
| -#' `after_stat()` replaces the old approaches of using either `stat()` or |
23 |
| -#' surrounding the variable names with `..`. |
24 |
| -#' |
25 |
| -#' @section Staging: |
| 22 | +#' @details |
| 23 | +#' # Staging |
26 | 24 | #' Below follows an overview of the three stages of evaluation and how aesthetic
|
27 | 25 | #' evaluation can be controlled.
|
28 | 26 | #'
|
29 | 27 | #' ## Stage 1: direct input
|
30 |
| -#' The default is to map at the beginning, using the layer data provided by |
31 |
| -#' the user. If you want to map directly from the layer data you should not do |
32 |
| -#' anything special. This is the only stage where the original layer data can |
33 |
| -#' be accessed. |
| 28 | +#' The default is to map at the beginning, using the layer data provided by |
| 29 | +#' the user. If you want to map directly from the layer data you should not do |
| 30 | +#' anything special. This is the only stage where the original layer data can |
| 31 | +#' be accessed. |
34 | 32 | #'
|
35 |
| -#' ```{r direct_input, eval = FALSE} |
| 33 | +#' ```r |
36 | 34 | #' # 'x' and 'y' are mapped directly
|
37 | 35 | #' ggplot(mtcars) + geom_point(aes(x = mpg, y = disp))
|
38 | 36 | #' ```
|
39 | 37 | #'
|
40 | 38 | #' ## Stage 2: after stat transformation
|
41 |
| -#' The second stage is after the data has been transformed by the layer |
42 |
| -#' stat. The most common example of mapping from stat transformed data is the |
43 |
| -#' height of bars in [geom_histogram()]: the height does not come from a |
44 |
| -#' variable in the underlying data, but is instead mapped to the `count` |
45 |
| -#' computed by [stat_bin()]. In order to map from stat transformed data you |
46 |
| -#' should use the `after_stat()` function to flag that evaluation of the |
47 |
| -#' aesthetic mapping should be postponed until after stat transformation. |
48 |
| -#' Evaluation after stat transformation will have access to the variables |
49 |
| -#' calculated by the stat, not the original mapped values. The 'computed |
50 |
| -#' variables' section in each stat lists which variables are available to |
51 |
| -#' access. |
| 39 | +#' The second stage is after the data has been transformed by the layer |
| 40 | +#' stat. The most common example of mapping from stat transformed data is the |
| 41 | +#' height of bars in [geom_histogram()]: the height does not come from a |
| 42 | +#' variable in the underlying data, but is instead mapped to the `count` |
| 43 | +#' computed by [stat_bin()]. In order to map from stat transformed data you |
| 44 | +#' should use the `after_stat()` function to flag that evaluation of the |
| 45 | +#' aesthetic mapping should be postponed until after stat transformation. |
| 46 | +#' Evaluation after stat transformation will have access to the variables |
| 47 | +#' calculated by the stat, not the original mapped values. The 'computed |
| 48 | +#' variables' section in each stat lists which variables are available to |
| 49 | +#' access. |
52 | 50 | #'
|
53 |
| -#' ```{r after_stat_transformation, eval = FALSE} |
| 51 | +#' ```r |
54 | 52 | #' # The 'y' values for the histogram are computed by the stat
|
55 | 53 | #' ggplot(faithful, aes(x = waiting)) +
|
56 | 54 | #' geom_histogram()
|
|
63 | 61 | #' ```
|
64 | 62 | #'
|
65 | 63 | #' ## Stage 3: after scale transformation
|
66 |
| -#' The third and last stage is after the data has been transformed and |
67 |
| -#' mapped by the plot scales. An example of mapping from scaled data could |
68 |
| -#' be to use a desaturated version of the stroke colour for fill. You should |
69 |
| -#' use `after_scale()` to flag evaluation of mapping for after data has been |
70 |
| -#' scaled. Evaluation after scaling will only have access to the final |
71 |
| -#' aesthetics of the layer (including non-mapped, default aesthetics). |
| 64 | +#' The third and last stage is after the data has been transformed and |
| 65 | +#' mapped by the plot scales. An example of mapping from scaled data could |
| 66 | +#' be to use a desaturated version of the stroke colour for fill. You should |
| 67 | +#' use `after_scale()` to flag evaluation of mapping for after data has been |
| 68 | +#' scaled. Evaluation after scaling will only have access to the final |
| 69 | +#' aesthetics of the layer (including non-mapped, default aesthetics). |
72 | 70 | #'
|
73 |
| -#' ```{r after_scale_transformation, eval = FALSE} |
| 71 | +#' ```r |
74 | 72 | #' # The exact colour is known after scale transformation
|
75 | 73 | #' ggplot(mpg, aes(cty, colour = factor(cyl))) +
|
76 | 74 | #' geom_density()
|
|
85 | 83 | #' data column for the stat, but remap it for the geom, you can use the
|
86 | 84 | #' `stage()` function to collect multiple mappings.
|
87 | 85 | #'
|
88 |
| -#' ```{r complex_staging, eval = FALSE} |
| 86 | +#' ```r |
89 | 87 | #' # Use stage to modify the scaled fill
|
90 | 88 | #' ggplot(mpg, aes(class, hwy)) +
|
91 | 89 | #' geom_boxplot(aes(fill = stage(class, after_scale = alpha(fill, 0.4))))
|
|
95 | 93 | #' ggplot(mpg, aes(class, displ)) +
|
96 | 94 | #' geom_violin() +
|
97 | 95 | #' stat_summary(
|
98 |
| -#' aes(y = stage(displ, after_stat = 8), |
99 |
| -#' label = after_stat(paste(mean, "±", sd))), |
| 96 | +#' aes( |
| 97 | +#' y = stage(displ, after_stat = 8), |
| 98 | +#' label = after_stat(paste(mean, "±", sd)) |
| 99 | +#' ), |
100 | 100 | #' geom = "text",
|
101 | 101 | #' fun.data = ~ round(data.frame(mean = mean(.x), sd = sd(.x)), 2)
|
102 | 102 | #' )
|
103 | 103 | #' ```
|
104 |
| -#' |
| 104 | +#' @note |
| 105 | +#' `after_stat()` replaces the old approaches of using either `stat()`, e.g. |
| 106 | +#' `stat(density)`, or surrounding the variable names with `..`, e.g. |
| 107 | +#' `..density..`. |
105 | 108 | #' @rdname aes_eval
|
106 | 109 | #' @name aes_eval
|
107 | 110 | #'
|
|
125 | 128 | #' # Making a proportional stacked density plot
|
126 | 129 | #' ggplot(mpg, aes(cty)) +
|
127 | 130 | #' geom_density(
|
128 |
| -#' aes(colour = factor(cyl), |
129 |
| -#' fill = after_scale(alpha(colour, 0.3)), |
130 |
| -#' y = after_stat(count / sum(n[!duplicated(group)]))), |
| 131 | +#' aes( |
| 132 | +#' colour = factor(cyl), |
| 133 | +#' fill = after_scale(alpha(colour, 0.3)), |
| 134 | +#' y = after_stat(count / sum(n[!duplicated(group)])) |
| 135 | +#' ), |
131 | 136 | #' position = "stack", bw = 1
|
132 | 137 | #' ) +
|
133 | 138 | #' geom_density(bw = 1)
|
|
136 | 141 | #' ggplot(mpg, aes(cty, colour = factor(cyl))) +
|
137 | 142 | #' geom_ribbon(
|
138 | 143 | #' stat = "density", outline.type = "upper",
|
139 |
| -#' aes(fill = after_scale(alpha(colour, 0.3)), |
140 |
| -#' ymin = after_stat(group), |
141 |
| -#' ymax = after_stat(group + ndensity)) |
| 144 | +#' aes( |
| 145 | +#' fill = after_scale(alpha(colour, 0.3)), |
| 146 | +#' ymin = after_stat(group), |
| 147 | +#' ymax = after_stat(group + ndensity) |
| 148 | +#' ) |
142 | 149 | #' )
|
143 | 150 | #'
|
144 | 151 | #' # Labelling a bar plot
|
145 | 152 | #' ggplot(mpg, aes(class)) +
|
146 | 153 | #' geom_bar() +
|
147 | 154 | #' geom_text(
|
148 |
| -#' aes(y = after_stat(count + 2), |
149 |
| -#' label = after_stat(count)), |
| 155 | +#' aes( |
| 156 | +#' y = after_stat(count + 2), |
| 157 | +#' label = after_stat(count) |
| 158 | +#' ), |
150 | 159 | #' stat = "count"
|
151 | 160 | #' )
|
152 | 161 | #'
|
|
155 | 164 | #' ggplot(mpg, aes(displ, class)) +
|
156 | 165 | #' geom_boxplot(outlier.shape = NA) +
|
157 | 166 | #' geom_text(
|
158 |
| -#' aes(label = after_stat(xmax), |
159 |
| -#' x = stage(displ, after_stat = xmax)), |
| 167 | +#' aes( |
| 168 | +#' label = after_stat(xmax), |
| 169 | +#' x = stage(displ, after_stat = xmax) |
| 170 | +#' ), |
160 | 171 | #' stat = "boxplot", hjust = -0.5
|
161 | 172 | #' )
|
162 | 173 | NULL
|
|
0 commit comments