以编程方式从列表中定义 ggplot2 美学,无需 aes_string

问题描述 投票:0回答:3

我有一个以编程方式构建并从其他函数传递过来的美学列表。它们可能包含任何美学组合,如 Alpha、颜色、填充等,这是事先未知的。

my_aes_list <- list(
    x = "cty",
    y = "hwy",
    col = "trans")

我可以使用

aes_string
从此列表中以编程方式创建美学:

library(ggplot2)
my_aes <- do.call(ggplot2::aes_string, my_aes_list)
ggplot(mpg) + geom_point(my_aes)

但是

aes_string()
在 ggplot2 3.0.0 中已被弃用,我想在未来版本中删除此代码之前替换它。

已记录的替代方案几个堆栈溢出问题表示使用

.data[[
代词。但是我不知道如何以任意的编程美学来使用它。

做类似的事情:

my_data_list <- list()
for (name in names(my_aes_list)) {
    my_data_list[[name]] <- .data[[my_aes_list[name]]]
}

显然在

aes
调用本身之外不起作用。我也尝试了
!!!
注射器但无济于事:

ggplot(mpg) + geom_point(aes(!!!my_aes_list))

似乎可以工作,但绘图不正确。

在这种情况下替换

aes_string
并调用
ggplot
的正确方法是什么?

ggplot2 nse aesthetics
3个回答
1
投票

有趣的问题;另一个潜在的解决方案是“enquo(ensym(list))”,然后使用

!!!
运算符,即

library(tidyverse)
library(rlang)

my_aes_list <- list(
  x = "cty",
  y = "hwy",
  col = "trans")

my_list_quos <- as_quosures(map(my_aes_list, sym), env = .GlobalEnv)
my_list_quos
#> <list_of<quosure>>
#> 
#> $x
#> <quosure>
#> expr: ^cty
#> env:  global
#> 
#> $y
#> <quosure>
#> expr: ^hwy
#> env:  global
#> 
#> $col
#> <quosure>
#> expr: ^trans
#> env:  global

ggplot(mpg) + geom_point(aes(!!!my_list_quos))

创建于 2023-06-26,使用 reprex v2.0.2


1
投票

我发现的一个解决方案是通过调用 .data[[:

“化解”代词
expr()
的评估

my_data_list <- lapply(my_aes_list, function(x) ggplot2::expr(.data[[x]]))

稍后,

ggplot2::aes()
透明地计算这些表达式:

my_aes <- do.call(ggplot2::aes, my_data_list)
ggplot(mpg) + geom_point(my_aes)

0
投票

tinycodet”R 包提供了

aes_pro()
,相当于
ggplot2::aes()
,只不过它使用公式输入而不是非标准评估。所以你可以执行以下操作:

library(ggplot2)
data("mpg", package = "ggplot2")
my_aes_list <- list(
  x = ~ cty,
  y = ~ hwy,
  col = ~ trans
)
my_aes <- do.call(tinycodet::aes_pro, my_aes_list) # create aes
ggplot(mpg) + geom_point(my_aes) # plot
© www.soinside.com 2019 - 2024. All rights reserved.