|
30 | 30 | #'
|
31 | 31 | #' This function also standardise aesthetic names by performing partial
|
32 | 32 | #' 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. |
34 | 46 | #'
|
35 | 47 | #' @param x,y,... List of name value pairs giving aesthetics to map to
|
36 | 48 | #' variables. The names for x and y aesthetics are typically omitted because
|
37 | 49 | #' 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. |
40 | 50 | #' @export
|
41 | 51 | #' @examples
|
42 | 52 | #' aes(x = mpg, y = wt)
|
|
57 | 67 | #'
|
58 | 68 | #' # Aesthetics supplied to ggplot() are used as defaults for every layer
|
59 | 69 | #' # 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) |
60 | 91 | aes <- function(x, y, ...) {
|
61 | 92 | exprs <- rlang::enquos(x = x, y = y, ...)
|
62 | 93 | is_missing <- vapply(exprs, rlang::quo_is_missing, logical(1))
|
|
0 commit comments