|
29 | 29 | #' performs partial name matching, converts color to colour, and old style R
|
30 | 30 | #' names to ggplot names (eg. pch to shape, cex to size)
|
31 | 31 | #'
|
32 |
| -#' @param x x value |
33 |
| -#' @param y y value |
34 |
| -#' @param ... List of name value pairs giving aesthetics to map. |
35 |
| -#' @seealso \code{\link{aes_string}} for passing quoted variable names. |
36 |
| -#" Useful when creating plots within user defined functions. Also, |
| 32 | +#' @param x,y,... List of name value pairs giving aesthetics to map. |
| 33 | +#' @family aesthetic generators |
| 34 | +#' @seealso See |
37 | 35 | #' \code{\link{aes_colour_fill_alpha}}, \code{\link{aes_group_order}},
|
38 | 36 | #' \code{\link{aes_linetype_size_shape}} and \code{\link{aes_position}}
|
39 | 37 | #' for more specific examples with different aesthetics.
|
@@ -81,30 +79,42 @@ is_position_aes <- function(vars) {
|
81 | 79 | aes_to_scale(vars) %in% c("x", "y")
|
82 | 80 | }
|
83 | 81 |
|
84 |
| -#' Generate aesthetic mappings from a string |
| 82 | +#' Generate aesthetic mappings from a string/quoted objects |
85 | 83 | #'
|
86 | 84 | #' Aesthetic mappings describe how variables in the data are mapped to visual
|
87 |
| -#' properties (aesthetics) of geoms. Compared to aes this function operates |
88 |
| -#' on strings rather than expressions. |
| 85 | +#' properties (aesthetics) of geoms. \code{\link{aes}} uses non-standard |
| 86 | +#' evaluation to capture the variable names. These two variants use |
| 87 | +#' regular evaluation, which is easier to use inside functions. |
89 | 88 | #'
|
90 |
| -#' \code{aes_string} is particularly useful when writing functions that create |
91 |
| -#' plots because you can use strings to define the aesthetic mappings, rather |
92 |
| -#' than having to mess around with expressions. |
| 89 | +#' \code{aes_string} and \code{aes_q} are particularly useful when writing |
| 90 | +#' functions that create plots because you can use strings or quoted |
| 91 | +#' names/calls to define the aesthetic mappings, rather than having to use |
| 92 | +#' \code{\link{substitute}} to generate a call to \code{aes()}. |
93 | 93 | #'
|
94 |
| -#' @param ... List of name value pairs |
| 94 | +#' @param x,y,... List of name value pairs |
| 95 | +#' @family aesthetic generators |
95 | 96 | #' @seealso \code{\link{aes}}
|
96 | 97 | #' @export
|
97 | 98 | #' @examples
|
98 |
| -#' aes_string(x = "mpg", y = "wt") |
99 |
| -#' aes(x = mpg, y = wt) |
100 |
| -aes_string <- function(...) { |
101 |
| - mapping <- list(...) |
102 |
| - mapping[sapply(mapping, is.null)] <- "NULL" |
| 99 | +#' # Threee ways of generating the same aesthetics |
| 100 | +#' aes(mpg, wt, col = cyl, fill = NULL) |
| 101 | +#' aes_string("mpg", "wt", col = "cyl", fill = NULL) |
| 102 | +#' aes_q(quote(mpg), quote(wt), col = quote(cyl), fill = NULL) |
| 103 | +aes_string <- function(x, y, ...) { |
| 104 | + mapping <- list(x = x, y = y, ...) |
| 105 | + mapping[vapply(mapping, is.null, logical(1))] <- "NULL" |
103 | 106 |
|
104 | 107 | parsed <- lapply(mapping, function(x) parse(text = x)[[1]])
|
105 | 108 | structure(rename_aes(parsed), class = "uneval")
|
106 | 109 | }
|
107 | 110 |
|
| 111 | +#' @rdname aes_string |
| 112 | +#' @export |
| 113 | +aes_q <- function(x, y, ...) { |
| 114 | + mapping <- list(x = x, y = y, ...) |
| 115 | + structure(rename_aes(mapping), class = "uneval") |
| 116 | +} |
| 117 | + |
108 | 118 | #' Given a character vector, create a set of identity mappings
|
109 | 119 | #'
|
110 | 120 | #' @param vars vector of variable names
|
|
0 commit comments