Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt suggestions of #5590 #5721

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion R/ggproto.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@
#' To explicitly call a methods in a parent, use
#' `ggproto_parent(Parent, self)`.
#'
#' @section Working with ggproto classes:
#' The ggproto objects constructed are build on top of environments, which has
#' some ramifications. Environments do not follow the 'copy on modify' semantics
#' one might be accustomed to in regular objects. Instead they have
#' ['modify in place'](https://adv-r.hadley.nz/names-values.html#env-modify)
#' semantics.
#'
Comment on lines +30 to +36
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this bit of my own volition, because I recall being a bit stumped by this when first working with ggproto objects.

#' @param _class Class name to assign to the object. This is stored as the class
#' attribute of the object. This is optional: if `NULL` (the default),
#' no class name will be added to the object.
#' @param _inherit ggproto object to inherit from. If `NULL`, don't
#' inherit from any object.
#' @param ... A list of members in the ggproto object.
#' @param ... A list of named members in the ggproto object. These can be
#' functions that become methods of the class or regular objects.
#' @seealso
#' The `r link_book("ggproto introduction section", "internals#sec-ggproto")`
#' @export
Expand Down
3 changes: 3 additions & 0 deletions R/stat-.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#' render the geom.
#' - `default_aes`: A list (generated by [aes()] of
#' default values for aesthetics.
#' - `dropped_aes` is a vecor of aesthetic names that are safe to drop after
#' statistical transformation. A classic example is the `weight` aesthetic
#' that is consumed during computation of the stat.
#'
#' See also the `r link_book("new stats section", "extensions#sec-new-stats")`
#' @rdname ggplot2-ggproto
Expand Down
3 changes: 3 additions & 0 deletions man/ggplot2-ggproto.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion man/ggproto.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions vignettes/extending-ggplot2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@ This stat illustrates another important point. If we want to make this stat usab
#| the kernel density estimate of the displacement."
StatDensityCommon <- ggproto("StatDensity2", Stat,
required_aes = "x",
default_aes = aes(y = stat(density)),
default_aes = aes(y = after_stat(density)),

compute_group = function(data, scales, bandwidth = 1) {
d <- density(data$x, bw = bandwidth)
data.frame(x = d$x, density = d$y)
}
)

ggplot(mpg, aes(displ, drv, colour = stat(density))) +
ggplot(mpg, aes(displ, drv, colour = after_stat(density))) +
stat_density_common(bandwidth = 1, geom = "point")
```

Expand Down Expand Up @@ -322,7 +322,7 @@ This is because each density is computed independently, and the estimated `x`s d
#| )
StatDensityCommon <- ggproto("StatDensityCommon", Stat,
required_aes = "x",
default_aes = aes(y = stat(density)),
default_aes = aes(y = after_stat(density)),

setup_params = function(data, params) {
min <- min(data$x) - 3 * params$bandwidth
Expand All @@ -344,7 +344,7 @@ StatDensityCommon <- ggproto("StatDensityCommon", Stat,

ggplot(mpg, aes(displ, fill = drv)) +
stat_density_common(bandwidth = 1, geom = "area", position = "stack")
ggplot(mpg, aes(displ, drv, fill = stat(density))) +
ggplot(mpg, aes(displ, drv, fill = after_stat(density))) +
stat_density_common(bandwidth = 1, geom = "raster")
```

Expand Down
Loading