Skip to content

Commit 66ebf0a

Browse files
authored
Improve aes() news and documentation (#2677)
* Improve aes() news and documentation Fixes #2676
1 parent a819813 commit 66ebf0a

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

NEWS.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ To be released as 2.3.0
1010
introduction to tidy evaluation can be found in the meta programming
1111
chapters in [Advanced R](https://adv-r.hadley.nz).
1212

13-
The primary developer facing change is that `aes()` is now a list of
14-
quosures (expression + environment pairs) rather than a list of symbols,
15-
and you'll need to take a different approach to extracting the information
16-
you need. A common symptom of this change are errors "undefined columns
17-
selected" or "invalid 'type' (list) of argument" (#2610).
13+
The primary developer facing change is that `aes()` now contains
14+
quosures (expression + environment pairs) rather than symbols, and you'll
15+
need to take a different approach to extracting the information you need.
16+
A common symptom of this change are errors "undefined columns selected" or
17+
"invalid 'type' (list) of argument" (#2610). As in the previous version,
18+
constants (like `aes(x = 1)` or `aes(colour = "smoothed")`) are stored
19+
as is.
1820

1921
In this version of ggplot2, you need to describe a mapping in a string,
2022
use `quo_name()` (for shorter labels) or `quo_text()` (if you want

R/aes.r

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ NULL
4848
#' they are so common; all other aesthetics must be named.
4949
#' @seealso [vars()] for another quoting function designed for
5050
#' faceting specifications.
51+
#' @return A list with class `uneval`. Components of the list are either
52+
#' quosures or constants.
5153
#' @export
5254
#' @examples
5355
#' aes(x = mpg, y = wt)
@@ -56,32 +58,36 @@ NULL
5658
#' # You can also map aesthetics to functions of variables
5759
#' aes(x = mpg ^ 2, y = wt / cyl)
5860
#'
61+
#' # Or to constants
62+
#' aes(x = 1, colour = "smooth")
63+
#'
5964
#' # Aesthetic names are automatically standardised
6065
#' aes(col = x)
6166
#' aes(fg = x)
6267
#' aes(color = x)
6368
#' aes(colour = x)
6469
#'
65-
#' # aes is almost always used with ggplot() or a layer
70+
#' # aes() is passed to either ggplot() or specific layer. Aesthetics supplied
71+
#' # to ggplot() are used as defaults for every layer.
6672
#' ggplot(mpg, aes(displ, hwy)) + geom_point()
6773
#' ggplot(mpg) + geom_point(aes(displ, hwy))
6874
#'
69-
#' # Aesthetics supplied to ggplot() are used as defaults for every layer
70-
#' # you can override them, or supply different aesthetics for each layer
71-
#'
72-
#'
73-
#' # aes() is a quoting function, so you need to use tidy evaluation
74-
#' # techniques to create wrappers around ggplot2 pipelines. The
75+
#' # Tidy evaluation ----------------------------------------------------
76+
#' # aes() automatically quotes all its arguments, so you need to use tidy
77+
#' # evaluation to create wrappers around ggplot2 pipelines. The
7578
#' # simplest case occurs when your wrapper takes dots:
7679
#' scatter_by <- function(data, ...) {
7780
#' ggplot(data) + geom_point(aes(...))
7881
#' }
7982
#' scatter_by(mtcars, disp, drat)
8083
#'
8184
#' # If your wrapper has a more specific interface with named arguments,
82-
#' # you need to use the "enquote and unquote" technique:
85+
#' # you need "enquote and unquote":
8386
#' scatter_by <- function(data, x, y) {
84-
#' ggplot(data) + geom_point(aes(!!enquo(x), !!enquo(y)))
87+
#' x <- enquo(x)
88+
#' y <- enquo(y)
89+
#'
90+
#' ggplot(data) + geom_point(aes(!!x, !!y))
8591
#' }
8692
#' scatter_by(mtcars, disp, drat)
8793
#'

man/aes.Rd

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

0 commit comments

Comments
 (0)