gpt4 book ai didi

r - R中的aes和aes_string(ggplot2)有什么区别

转载 作者:行者123 更新时间:2023-12-03 20:41:32 27 4
gpt4 key购买 nike

由于缺乏信息学背景知识,我很难理解ggplot2中aesaes_string之间的差异及其对日常使用的影响。

从描述(?aes_string)中,我能够理解这两个describe how variables in the data are mapped to visual properties (aesthetics) of geom

此外,据说aes uses non-standard evaluation to capture the variable names.aes_string使用regular evaluation

从示例代码中可以明显看出,两者都产生相同的输出(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 evaluationHadley Wickham in his book Advanced R描述为一种方法,不仅可以调用函数参数的值,还可以调用产生它们的代码。

我会假设相反的 regular evaluation仅调用函数中的值,但是我没有找到证实这一假设的来源。此外,我不清楚这两者之间有何区别,以及为什么在使用该包装时这与我有​​关。

inside-R website上提到 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.

但是从这个意义上说,我不清楚为什么我每次使用 aes时都应该使用 aes_string而不总是选择 ggplot2 ...从这个意义上讲,这将有助于我找到一些有关这些概念和实用性的说明。日常使用的提示。

最佳答案

aes为您节省了一些输入,因为您不需要引号。就这些。您当然可以随时使用aes_string。如果要以编程方式传递变量名称,则应使用aes_string

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

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


为了比较:

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


这些符号在稍后阶段进行评估。

aes_string使用 parse实现相同的目的:

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

关于r - R中的aes和aes_string(ggplot2)有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28897577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com