What is the difference between aes and aes_string (ggplot2) in R
Asked Answered
O

1

26

With missing background in informatics I have difficulties to understand the differences between aes and aes_string in ggplot2 and its implications for daily usage.

From the description (?aes_string) I was able to understand that both describe how variables in the data are mapped to visual properties (aesthetics) of geom.

Furthermore it is said that aes uses non-standard evaluation to capture the variable names. whereas aes_string uses regular evaluation.

From example code it is clear that both produce the same output (a list of unevaluated expressions):

> aes_string(x = "mpg", y = "wt")
List of 2
 $ x: symbol mpg
 $ y: symbol wt
> aes(x = mpg, y = wt)
List of 2
 $ x: symbol mpg
 $ y: symbol wt

Non-standard evaluation is described by Hadley Wickham in his book Advanced R as a method to not only call the values of a functions argument but also the code that produced them.

I would assume that regular evaluation in opposition only calls the values from the function, but I did not find a source to confirm this assumption. Furthermore it is unclear to me how those two differ and why this should be relevant to me when I use the package.

On the inside-R website it is mentioned that aes_string is particularly useful when writing functions that create plots because you can use strings to define the aesthetic mappings, rather than having to mess around with expressions.

But in that sense it is unclear to me why I should ever use aes and not opt always for aes_string whenever using ggplot2... In that sense it would help me to find some clarifications on those concepts and a practical hint for daily usage.

Oppress answered 6/3, 2015 at 11:11 Comment(1)
Please note that aes_string() is now soft-deprecated, and .data[[]] is preferred. So previously: aes_string(x = chr_variable) but now: aes(x = .data[[chr_variable]])Senescent
I
25

aes saves you some typing as you don't need the quotes. That is all. You are of course free to always use aes_string. You should use aes_string if you want to pass the variable names programmatically.

Internally aes uses match.call for the non-standard evaluation. Here is a simple example for illustration:

fun <- function(x, y) as.list(match.call())
str(fun(a, b))
#List of 3
# $  : symbol fun
# $ x: symbol a
# $ y: symbol b

For comparison:

library(ggplot2)
str(aes(x = a, y = b))
#List of 2
# $ x: symbol a
# $ y: symbol b

The symbols are evaluated at a later stage.

aes_string uses parse to achieve the same:

str(aes_string(x = "a", y = "b"))
#List of 2
# $ x: symbol a
# $ y: symbol b
Infusorian answered 6/3, 2015 at 11:28 Comment(2)
unfortunately, that's not so simple: ggplot does not have the same plotting-behaviour depending on whether you used aes or aes_string. It messes up the memory in different ways and plots different things... see for instance #23439766. ggplot is a huge problem in itselfPosehn
"You should use aes_string if you want to pass the variable names programmatically" is no longer true since ggplot 3.0.0 and its support of !!. See https://mcmap.net/q/82523/-how-to-use-a-variable-to-specify-column-name-in-ggplot .Fairchild

© 2022 - 2024 — McMap. All rights reserved.