Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ggplot theme formatting be saved as an object?

Tags:

r

ggplot2

TL;DR: How do I save the plotting axis text and sizes and et cetera an object to make my code shorter?

Say for example I wanted to plot different data with potentially different geoms but use the same axis text sizing and titles.

It would look like this in made up code

ggplot(data = df, aes(x = x, y = y) + geom_line() +
    ylab("my y axis") +
    xlab("my x axis") +
    opts(title = "my title") +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=14,face="bold"))


ggplot(data = new_df, aes(x = whatever, y = something) + geom_anythingelse() +
    ylab("my y axis") +
    xlab("my x axis") +
    opts(title = "my title") +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=14,face="bold"))

#...

How or can I save

my_theme <-   ylab("my y axis") +
              xlab("my x axis") +
              opts(title = "my title") +
              theme(axis.text=element_text(size=20),
              axis.title=element_text(size=14,face="bold"))

as its own object to add to ggplot when I like. Is ggplot flexible enough to accommodate my need here?

ggplot(data = df, aes(x = x, y = y) + geom_point() +
my_theme

Does this question violate the object naming philosophy that ggplot was built on?

like image 471
cylondude Avatar asked Apr 19 '14 18:04

cylondude


People also ask

How to create your own theme in ggplot2?

Hence in order to create our own one, it is enough to define a function my_theme that calls the generic theme () function and where we define all the parameters as we like. Let us start with a nice default ggplot2 plot. For instance, we consider the usual iris dataset and we plot a histogram of Petal.Length:

How to change the default look of plots created with ggplot2?

Plots created with ggplot2 have a fairly distinctive default look with grey background and white gridlines. It is possible to change it by using the different themes available in ggplot, but still sometimes there are some default options that we always want to get rid of and that every time we need to specify.

How do I standardize a ggplot?

Standardization: Once you have created a theme, you can apply it to all ggplot graphs. It will work for line graphs, bar graphs, faceted graphs, etc. If you are making a theme that others will use (Airbnb and BBC examples above), your graphs will be consistent across the company.

How to change the default theme of a plot in Python?

When we want to change the default theme, for example to the black and white theme, we just add at the end of the plot theme_bw (). So basically a theme is just a function with a number of arguments set by default, such as background color, size and style of axis text and labels, legend, etc.


2 Answers

Stack Overflow user baptiste commented on my question with the info nugget that the labeling I want to save can be saved as a list. He mentions that geoms and scales can be put into a list but I only tested labels. As I tested it, themes did not work if I wrote them into a list and had to be saved seperately.

my_labels <- list(ylab("my y axis"),
             xlab("my x axis"),
             ggtitle("my title")
             )

my_theme    <- theme(axis.text=element_text(size=20),
                     axis.title=element_text(size=14,face="bold"))

ggplot(data = df, aes(x = x, y = y) + geom_line() + my_labels + my_theme
like image 76
cylondude Avatar answered Sep 18 '22 11:09

cylondude


You can make a theme object without any problems, e.g:

mytheme<-theme(panel.background=element_rect(colour="green"))

It is even easier, if this is your standard theme to type

old_theme<- theme_update(panel.background=element_rect(colour="green"))

In the former case you write:

ggplot(...)+mytheme

while in the latter, because your custom theme is now the standard theme, it is only necessary to type:

ggplot(...)
like image 39
Mali Remorker Avatar answered Sep 17 '22 11:09

Mali Remorker