Layer :: mapping

Published by onesixx on

 

 

 

 

 

 

 

ggplot2 :: aes_string vs. aes

https://stackoverflow.com/questions/28897577/what-is-the-difference-between-aes-and-aes-string-ggplot2-in-r

https://nsaunders.wordpress.com/2013/02/26/rggplot2-tip-aes_string/

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.

 

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_stringwhenever using ggplot2… In that sense it would help me to find some clarifications on those concepts and a practical hint for daily usage.
r ggplot2 aesthetics

 

aes(x = mpg, y = wt) 과 aes_string(x = “mpg”, y = “wt”) 의 결과는 같다.

 

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:

 

 

aes

aes_string 

 

일반 function

match.call for the non-standard evaluation

fun <- function(x, y){
         as.list(match.call())
       }

fun(a, b) %>% str
#List of 3
# $ : symbol fun
# $ x: symbol a
# $ y: symbol b

aes()

따옴표 없이 사용. 

library(ggplot2)
aes(x=a, y=b) %>% str
#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:

변수명을 넘김 

aes_string(x="a", y="b") %>% str
#List of 2
# $ x: symbol a
# $ y: symbol b

 

 

 

 

 

 

Categories: ggplot2

onesixx

Blog Owner

Leave a Reply

Your email address will not be published.