Skip to content

Commit ce2c873

Browse files
committed
Add aes_q
1 parent 95dd156 commit ce2c873

File tree

5 files changed

+56
-33
lines changed

5 files changed

+56
-33
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export("%+replace%")
179179
export(aes)
180180
export(aes_all)
181181
export(aes_auto)
182+
export(aes_q)
182183
export(aes_string)
183184
export(annotate)
184185
export(annotation_custom)

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
ggplot2 0.9.3.1.99
22
----------------------------------------------------------------
33

4+
* New `aes_q()` function to generate aesthetic specifications from
5+
quoted calls/names. `aes_string()` uses names `x` and `y` for first
6+
two unnamed arguments.
7+
48
* Marginal improvements to `theme_bw` and `theme_classic` (@jiho, #934)
59

610
* `stat_ellipse()` adds data ellipses. It supports bivariate normal and t distributions,

R/aes.r

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929
#' performs partial name matching, converts color to colour, and old style R
3030
#' names to ggplot names (eg. pch to shape, cex to size)
3131
#'
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
3735
#' \code{\link{aes_colour_fill_alpha}}, \code{\link{aes_group_order}},
3836
#' \code{\link{aes_linetype_size_shape}} and \code{\link{aes_position}}
3937
#' for more specific examples with different aesthetics.
@@ -81,30 +79,42 @@ is_position_aes <- function(vars) {
8179
aes_to_scale(vars) %in% c("x", "y")
8280
}
8381

84-
#' Generate aesthetic mappings from a string
82+
#' Generate aesthetic mappings from a string/quoted objects
8583
#'
8684
#' 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.
8988
#'
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()}.
9393
#'
94-
#' @param ... List of name value pairs
94+
#' @param x,y,... List of name value pairs
95+
#' @family aesthetic generators
9596
#' @seealso \code{\link{aes}}
9697
#' @export
9798
#' @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"
103106

104107
parsed <- lapply(mapping, function(x) parse(text = x)[[1]])
105108
structure(rename_aes(parsed), class = "uneval")
106109
}
107110

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+
108118
#' Given a character vector, create a set of identity mappings
109119
#'
110120
#' @param vars vector of variable names

man/aes.Rd

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ mapped to visual properties (aesthetics) of geoms.}
77
aes(x, y, ...)
88
}
99
\arguments{
10-
\item{x}{x value}
11-
12-
\item{y}{y value}
13-
14-
\item{...}{List of name value pairs giving aesthetics to map.}
10+
\item{x,y,...}{List of name value pairs giving aesthetics to map.}
1511
}
1612
\description{
1713
\code{aes} creates a list of unevaluated expressions. This function also
@@ -23,9 +19,12 @@ aes(x = mpg, y = wt)
2319
aes(x = mpg ^ 2, y = wt / cyl)
2420
}
2521
\seealso{
26-
\code{\link{aes_string}} for passing quoted variable names.
22+
See
2723
\code{\link{aes_colour_fill_alpha}}, \code{\link{aes_group_order}},
2824
\code{\link{aes_linetype_size_shape}} and \code{\link{aes_position}}
2925
for more specific examples with different aesthetics.
26+
27+
Other aesthetic generators: \code{\link{aes_q}},
28+
\code{\link{aes_string}}
3029
}
3130

man/aes_string.Rd

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
% Generated by roxygen2 (4.0.0): do not edit by hand
22
\name{aes_string}
3+
\alias{aes_q}
34
\alias{aes_string}
4-
\title{Generate aesthetic mappings from a string}
5+
\title{Generate aesthetic mappings from a string/quoted objects}
56
\usage{
6-
aes_string(...)
7+
aes_string(x, y, ...)
8+
9+
aes_q(x, y, ...)
710
}
811
\arguments{
9-
\item{...}{List of name value pairs}
12+
\item{x,y,...}{List of name value pairs}
1013
}
1114
\description{
1215
Aesthetic mappings describe how variables in the data are mapped to visual
13-
properties (aesthetics) of geoms. Compared to aes this function operates
14-
on strings rather than expressions.
16+
properties (aesthetics) of geoms. \code{\link{aes}} uses non-standard
17+
evaluation to capture the variable names. These two variants use
18+
regular evaluation, which is easier to use inside functions.
1519
}
1620
\details{
17-
\code{aes_string} is particularly useful when writing functions that create
18-
plots because you can use strings to define the aesthetic mappings, rather
19-
than having to mess around with expressions.
21+
\code{aes_string} and \code{aes_q} are particularly useful when writing
22+
functions that create plots because you can use strings or quoted
23+
names/calls to define the aesthetic mappings, rather than having to use
24+
\code{\link{substitute}} to generate a call to \code{aes()}.
2025
}
2126
\examples{
22-
aes_string(x = "mpg", y = "wt")
23-
aes(x = mpg, y = wt)
27+
# Threee ways of generating the same aesthetics
28+
aes(mpg, wt, col = cyl, fill = NULL)
29+
aes_string("mpg", "wt", col = "cyl", fill = NULL)
30+
aes_q(quote(mpg), quote(wt), col = quote(cyl), fill = NULL)
2431
}
2532
\seealso{
2633
\code{\link{aes}}
34+
35+
Other aesthetic generators: \code{\link{aes}}
2736
}
2837

0 commit comments

Comments
 (0)