Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine/Merge two ggplot aesthetics

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.

like image 800
papirrin Avatar asked Nov 19 '13 23:11

papirrin


1 Answers

> 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"
like image 125
IRTFM Avatar answered Sep 19 '22 12:09

IRTFM