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?
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:
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.
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.
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.
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
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(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With