Bram Vanroy
Bram Vanroy

Reputation: 28437

Assign the same value to multiple theme elements in ggplot2

I feel like this is quite an easy question, but I couldn't find it asked anywhere. Nor could I find anything in the docs.

Is it possible in ggplot2 to assign the same value to multiple theme elements? For instance, I can write my theme declaration as follows:

 theme(axis.text = element_text(colour = "gray25"), 
        axis.text.x = element_text(angle = 45, hjust = 1),
        line = element_line(colour = "gray25"),
        strip.text = element_text(face = "bold"),
        legend.title = element_text(colour = "gray25"),
        legend.text = element_text(colour = "gray25"),
        plot.title = element_text(colour = "gray25", face="bold",vjust=2))

But this seems highly repetetive. Isn't there something like a conjunction to assign the same value to multiple elements? Something like this for instance.

   theme(axis.text & legend.title & legend.text & plot.title = element_text(colour = "gray25"),
        labels bold 
        axis.text.x = element_text(angle = 45, hjust = 1),
        strip.text = element_text(face = "bold"),
        plot.title = element_text(face="bold",vjust=2))

Upvotes: 1

Views: 607

Answers (1)

Henrik
Henrik

Reputation: 67778

In ?theme, you find that "Theme elements can inherit properties from other theme elements". For all individual theme elements, you find which element they inherit from. Thus, you can try to change as many properties as possible in the 'top level' in order to avoid redundancy. You can read more in the official theme vignette, where you also a find visualization of the inheritance between elements: enter image description here

If you have a theme which you wish to use repeatedly (e.g. for specific journals or publishers), you may customize and save your own themes. For example, you may start from the default theme, make your changes, save it, and use it just by adding + theme_bw_custom (or whatever you name it) to the end of your plotting code.

See also and ggthemes, which may serve as a starting point when creating your own themes.

Upvotes: 2

Related Questions