Ggplot2: using options for multiple charts

I would like to create 10 graphs that have different data, but the same optical appearance. As an example, Id can change the color of the grid line for each graph. This can be done by adding

+ opts(panel.grid.major = theme_line(colour = "white") 

for each plot definition. However, when I now decided to change the background color to say "grey25", Id had to modify each plot separately. This seems like too much work .;)

So, I thought about doing something like

 opt1 <- '+ opts(panel.grid.major = theme_line(colour = "white")' 

and then define each graph as

 pl_x <- pl_x + opt1 pl_y <- pl_y + opt1 ... 

Other options (fields, fonts, scales, ...) can then be added to opt1. However, this does not work (error message when trying to print pl_x). Does anyone maybe know how to accomplish what I'd like to do?

I also played with theme_set and theme_update, but this did not mean that none of my jobs worked anymore unless I completely restarted R.

+3
r layout plot ggplot2
source share
1 answer

You do not need to add a + sign.

 opt <- opts(panel.grid.major = theme_line(colour = "white")) pl_x <- pl_x + opt 

Although this does not work:

 opt <- opts(...) + scale_y_continuous(..) 

It does:

 opt <- opts(...) syc <- scale_y_continuous(...) pl_x <- pl_x + opt + syc 

And thanks to Hadley's example, this also works:

 opt <- list(opts(...),scale_y_continuous(...)) 

Note Since version 0.9.2 opts been replaced by theme .

+3
source share

All Articles