Skip to content

Commit c8df060

Browse files
authored
Adopt suggestions of #5590 (#5721)
* document `Stat$dropped_aes` * replace `stat()` with `after_stat()` * tweak ggproto docs
1 parent 1f49bb6 commit c8df060

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

R/ggproto.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@
2727
#' To explicitly call a methods in a parent, use
2828
#' `ggproto_parent(Parent, self)`.
2929
#'
30+
#' @section Working with ggproto classes:
31+
#' The ggproto objects constructed are build on top of environments, which has
32+
#' some ramifications. Environments do not follow the 'copy on modify' semantics
33+
#' one might be accustomed to in regular objects. Instead they have
34+
#' ['modify in place'](https://adv-r.hadley.nz/names-values.html#env-modify)
35+
#' semantics.
36+
#'
3037
#' @param _class Class name to assign to the object. This is stored as the class
3138
#' attribute of the object. This is optional: if `NULL` (the default),
3239
#' no class name will be added to the object.
3340
#' @param _inherit ggproto object to inherit from. If `NULL`, don't
3441
#' inherit from any object.
35-
#' @param ... A list of members in the ggproto object.
42+
#' @param ... A list of named members in the ggproto object. These can be
43+
#' functions that become methods of the class or regular objects.
3644
#' @seealso
3745
#' The `r link_book("ggproto introduction section", "internals#sec-ggproto")`
3846
#' @export

R/stat-.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
#' render the geom.
4646
#' - `default_aes`: A list (generated by [aes()] of
4747
#' default values for aesthetics.
48+
#' - `dropped_aes` is a vecor of aesthetic names that are safe to drop after
49+
#' statistical transformation. A classic example is the `weight` aesthetic
50+
#' that is consumed during computation of the stat.
4851
#'
4952
#' See also the `r link_book("new stats section", "extensions#sec-new-stats")`
5053
#' @rdname ggplot2-ggproto

man/ggplot2-ggproto.Rd

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

man/ggproto.Rd

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/extending-ggplot2.Rmd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,15 @@ This stat illustrates another important point. If we want to make this stat usab
284284
#| the kernel density estimate of the displacement."
285285
StatDensityCommon <- ggproto("StatDensity2", Stat,
286286
required_aes = "x",
287-
default_aes = aes(y = stat(density)),
287+
default_aes = aes(y = after_stat(density)),
288288
289289
compute_group = function(data, scales, bandwidth = 1) {
290290
d <- density(data$x, bw = bandwidth)
291291
data.frame(x = d$x, density = d$y)
292292
}
293293
)
294294
295-
ggplot(mpg, aes(displ, drv, colour = stat(density))) +
295+
ggplot(mpg, aes(displ, drv, colour = after_stat(density))) +
296296
stat_density_common(bandwidth = 1, geom = "point")
297297
```
298298

@@ -322,7 +322,7 @@ This is because each density is computed independently, and the estimated `x`s d
322322
#| )
323323
StatDensityCommon <- ggproto("StatDensityCommon", Stat,
324324
required_aes = "x",
325-
default_aes = aes(y = stat(density)),
325+
default_aes = aes(y = after_stat(density)),
326326
327327
setup_params = function(data, params) {
328328
min <- min(data$x) - 3 * params$bandwidth
@@ -344,7 +344,7 @@ StatDensityCommon <- ggproto("StatDensityCommon", Stat,
344344
345345
ggplot(mpg, aes(displ, fill = drv)) +
346346
stat_density_common(bandwidth = 1, geom = "area", position = "stack")
347-
ggplot(mpg, aes(displ, drv, fill = stat(density))) +
347+
ggplot(mpg, aes(displ, drv, fill = after_stat(density))) +
348348
stat_density_common(bandwidth = 1, geom = "raster")
349349
```
350350

0 commit comments

Comments
 (0)