I am plotting tons of graphs which essentially use the same type of formatting. Just wondering if it possible to store these layers in a variable and reuse them.
Approach 1 (does not work)
t <- layer1() + layer2()
ggplot(df,aes(x,y)) + t
Approach 2 (works but not very elegant)
t <- function(x) x + layer1() + layer2()
t(ggplot(df,aes(x,y))
Any suggestion along the lines of approach 1?
Thanks!
layer1()
andlayer2()
are supposed to be. What do they return? What do they do? Normally, I would just save the result of ageom_*
call in a variable and add it in later.t()
.scale_*
andtheme()
aren't generally called "layers" in ggplot. That word typically refers to geoms and stats.