Create templates using ggplot2 syntax?

I wonder if it is possible to create a similar set of numbers in ggplot2 and somehow change the data. For example, I can create a function to perform this task:

plot1 <- function(data) ggplot(data) + geom_line(aes(x,y)) + theme_bw() plot1(data) plot1(newdata) 

But is it possible to save and reuse a set of components in some way? (obviously this does not work):

 g <- geom_line(aes(x,y)) + theme_bw() ggplot(data) + g ggplot(newdata) + g 
+6
source share
1 answer

The +.gg methods are described +.gg

These are %+% and %+replace% , which will update / replace elements in ggplot and themes

eg,

 p <- ggplot(mtcars, aes(x =wt, y = mpg,colour = hp)) + geom_point() # change the variable mapped to y p %+% aes(y = am) # change the data set p %+% mtcars[1:10,] 

Or you can combine items as a list

eg,

 # g <- list(geom_line(aes(x,y)),theme_bw()) ggplot(data) + g 
+14
source

Source: https://habr.com/ru/post/945740/


All Articles