Question

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.

Was it helpful?

Solution

> 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"

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top