Skip to content

Commit 30f366d

Browse files
committed
Document tidy eval for mappings with aes()
1 parent 6647b31 commit 30f366d

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

R/aes.r

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,23 @@ NULL
3030
#'
3131
#' This function also standardise aesthetic names by performing partial
3232
#' matching, converting color to colour, and translating old style R names to
33-
#' ggplot names (eg. pch to shape, cex to size)
33+
#' ggplot names (eg. pch to shape, cex to size).
34+
#'
35+
#'
36+
#' @section Quasiquotation:
37+
#'
38+
#' `aes()` is a [quoting function][rlang::quotation]. This means that
39+
#' its inputs are quoted to be evaluated in the context of the
40+
#' data. This makes it easy to work with variables from the data frame
41+
#' because you can name those directly. The flip side is that you have
42+
#' to use [quasiquotation][rlang::quasiquotation] to program with
43+
#' `aes()`. See a tidy evaluation tutorial such as the [dplyr
44+
#' programming vignette](http://dplyr.tidyverse.org/articles/programming.html)
45+
#' to learn more about these techniques.
3446
#'
3547
#' @param x,y,... List of name value pairs giving aesthetics to map to
3648
#' variables. The names for x and y aesthetics are typically omitted because
3749
#' they are so common; all other aesthetics must be named.
38-
#' @seealso See [aes_()] for a version of `aes` that is
39-
#' more suitable for programming with.
4050
#' @export
4151
#' @examples
4252
#' aes(x = mpg, y = wt)
@@ -57,6 +67,27 @@ NULL
5767
#'
5868
#' # Aesthetics supplied to ggplot() are used as defaults for every layer
5969
#' # you can override them, or supply different aesthetics for each layer
70+
#'
71+
#'
72+
#' # aes() is a quoting function, so you need to use tidy evaluation
73+
#' # techniques to create wrappers around ggplot2 pipelines. The
74+
#' # simplest case occurs when your wrapper takes dots:
75+
#' scatter_by <- function(data, ...) {
76+
#' ggplot(data) + geom_point(aes(...))
77+
#' }
78+
#' scatter_by(mtcars, disp, drat)
79+
#'
80+
#' # If your wrapper has a more specific interface with named arguments,
81+
#' # you need to use the "enquote and unquote" technique:
82+
#' scatter_by <- function(data, x, y) {
83+
#' ggplot(data) + geom_point(aes(!!enquo(x), !!enquo(y)))
84+
#' }
85+
#' scatter_by(mtcars, disp, drat)
86+
#'
87+
#' # Note that users of your wrapper can use their own functions in the
88+
#' # quoted expressions and all will resolve as it should!
89+
#' cut3 <- function(x) cut_number(x, 3)
90+
#' scatter_by(mtcars, cut3(disp), drat)
6091
aes <- function(x, y, ...) {
6192
exprs <- rlang::enquos(x = x, y = y, ...)
6293
is_missing <- vapply(exprs, rlang::quo_is_missing, logical(1))

man/aes.Rd

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

0 commit comments

Comments
 (0)