Skip to content

Commit b88f7b2

Browse files
committed
Update suggestions
1 parent 04b42de commit b88f7b2

File tree

2 files changed

+82
-60
lines changed

2 files changed

+82
-60
lines changed

R/aes-evaluation.r

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#' three functions to control at which stage aesthetics should be evaluated.
77
#'
88
#' @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)))
1011
#'
1112
#' @param x <[`data-masking`][rlang::topic-data-mask]> An aesthetic expression
1213
#' using variables calculated by the stat (`after_stat()`) or layer aesthetics
@@ -18,39 +19,36 @@
1819
#' @param after_scale <[`data-masking`][rlang::topic-data-mask]> An aesthetic
1920
#' expression using layer aesthetics.
2021
#'
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
2624
#' Below follows an overview of the three stages of evaluation and how aesthetic
2725
#' evaluation can be controlled.
2826
#'
2927
#' ## 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.
3432
#'
35-
#' ```{r direct_input, eval = FALSE}
33+
#' ```r
3634
#' # 'x' and 'y' are mapped directly
3735
#' ggplot(mtcars) + geom_point(aes(x = mpg, y = disp))
3836
#' ```
3937
#'
4038
#' ## 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.
5250
#'
53-
#' ```{r after_stat_transformation, eval = FALSE}
51+
#' ```r
5452
#' # The 'y' values for the histogram are computed by the stat
5553
#' ggplot(faithful, aes(x = waiting)) +
5654
#' geom_histogram()
@@ -63,14 +61,14 @@
6361
#' ```
6462
#'
6563
#' ## 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).
7270
#'
73-
#' ```{r after_scale_transformation, eval = FALSE}
71+
#' ```r
7472
#' # The exact colour is known after scale transformation
7573
#' ggplot(mpg, aes(cty, colour = factor(cyl))) +
7674
#' geom_density()
@@ -85,7 +83,7 @@
8583
#' data column for the stat, but remap it for the geom, you can use the
8684
#' `stage()` function to collect multiple mappings.
8785
#'
88-
#' ```{r complex_staging, eval = FALSE}
86+
#' ```r
8987
#' # Use stage to modify the scaled fill
9088
#' ggplot(mpg, aes(class, hwy)) +
9189
#' geom_boxplot(aes(fill = stage(class, after_scale = alpha(fill, 0.4))))
@@ -95,13 +93,18 @@
9593
#' ggplot(mpg, aes(class, displ)) +
9694
#' geom_violin() +
9795
#' 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+
#' ),
100100
#' geom = "text",
101101
#' fun.data = ~ round(data.frame(mean = mean(.x), sd = sd(.x)), 2)
102102
#' )
103103
#' ```
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..`.
105108
#' @rdname aes_eval
106109
#' @name aes_eval
107110
#'
@@ -125,9 +128,11 @@
125128
#' # Making a proportional stacked density plot
126129
#' ggplot(mpg, aes(cty)) +
127130
#' 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+
#' ),
131136
#' position = "stack", bw = 1
132137
#' ) +
133138
#' geom_density(bw = 1)
@@ -136,17 +141,21 @@
136141
#' ggplot(mpg, aes(cty, colour = factor(cyl))) +
137142
#' geom_ribbon(
138143
#' 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+
#' )
142149
#' )
143150
#'
144151
#' # Labelling a bar plot
145152
#' ggplot(mpg, aes(class)) +
146153
#' geom_bar() +
147154
#' 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+
#' ),
150159
#' stat = "count"
151160
#' )
152161
#'
@@ -155,8 +164,10 @@
155164
#' ggplot(mpg, aes(displ, class)) +
156165
#' geom_boxplot(outlier.shape = NA) +
157166
#' 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+
#' ),
160171
#' stat = "boxplot", hjust = -0.5
161172
#' )
162173
NULL

man/aes_eval.Rd

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

0 commit comments

Comments
 (0)