Let's say I have two ggplot aesthetics:

a.1 <- aes(v.1=1, v.2=2)
a.2 <- aes(v.3=3)

Is there a way to combine the already built aesthetics? In my example it would be something like:

a.merged <- aes(v.1=2, v.2=2,v.3=3)

I know that aes_string can be used to build an aesthetic from a character vector, that can be concatenated, but I am looking at the case where the two aesthetics are already built and I want to avoid having to convert them to characters first.

有帮助吗?

解决方案

> c(a.1,a.2)
$v.1
[1] 1

$v.2
[1] 2

$v.3
[1] 3

aes objects are "unevaluated expressions" and the c() function works as expected, depending on your definition of "expected". To be safe you probably need to add back the class that gets stripped by c():

a.merged <- c(a.1,a.2)
class(a.merged) <- "uneval"

If you want to do it in one step then the function modifyList will not strip the "name"-less attributes:

> modifyList(a.1, a.2)
List of 3
 $ v.1: num 1
 $ v.2: num 2
 $ v.3: num 3
> attributes(modifyList(a.1, a.2))
$names
[1] "v.1" "v.2" "v.3"

$class
[1] "uneval"

其他提示

library(ggplot2)

a.1 <- aes(v.1=1, v.2=2)
a.2 <- aes(v.3=3)

modifyList(a.1, a.2)
Aesthetic mapping: 
* `v.1` -> 1
* `v.2` -> 2
* `v.3` -> 3

modifyList doesn't mutate its arguments, despite the name. Thanks to Simon.S.A. for suggesting this approach in the comments.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top