R中的aes和aes_string(ggplot2)有什么区别? [英] What is the difference between aes and aes_string (ggplot2) in R

查看:4046
本文介绍了R中的aes和aes_string(ggplot2)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于信息学背景的缺失,我很难理解ggplot2中 aes aes_string 之间的区别及其含义用于日常使用。



从描述中(?aes_string ),我明白了描述数据中的变量如何映射到geom的可视属性(美学)

此外,据说 aes使用非标准评估来捕获变量名称。 aes_string 使用定期评估



产生相同的输出(未评估表达式列表):

 > ; aes_string(x =mpg,y =wt)
2
$ x:符号mpg
$ y:符号wt
> aes(x = mpg,y = wt)
2
$ x:符号mpg
$ y:符号wt


非标准评估 Hadley Wickham在他的书 Advanced R 中作为一种方法,不仅可以调用函数参数的值还包括产生它们的代码。



我会假设常规评估在对立面中只会调用函数的值,但我没有找到来源来证实这一假设。此外,我不清楚这两者的不同之处,以及在使用包装时为什么这与我有关。



inside-R网站中提到 aes_string在编写创建图的函数时特别有用,因为您可以使用字符串来定义审美映射,而不必乱用表达式。



但是在这个意义上,我不清楚为什么我会使用 aes 而不是每当使用 ggplot2 时,始终选择 aes_string ...从这个意义上讲,这将帮助我找到对这些概念的一些澄清并提供日常使用的实用提示。 引号。就这些。您当然可以随时使用 aes_string 。如果你想以编程方式传递变量名,你应该使用 aes_string



内部 aes 使用 match.call 来进行非标准评估。下面是一个简单的例子:

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

比较:

<$ p $
$($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b#$ y:符号b

这些符号是在稍后阶段评估的。



aes_string 使用 parse 实现相同:

  str(aes_string(x =a,y =b))
#2
#$ x :符号a
#$ y:符号b


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.

解决方案

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

这篇关于R中的aes和aes_string(ggplot2)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆