9.5 Creating Your Own Themes
9.5.2 Solution
You can create your own theme by adding elements to an existing theme (Figure 9.10):
library(gcookbook) # Load gcookbook for the heightweight data set
# Start with theme_bw() and modify a few things
theme_bw() +
mytheme <- theme(
text = element_text(colour = "red"),
axis.title = element_text(size = rel(1.25))
)
# Create the base plot
ggplot(heightweight, aes(x = ageYear, y = heightIn)) +
hw_plot <- geom_point()
# Plot with the modified theme we created
+
hw_plot mytheme
9.5.3 Discussion
With ggplot2, you can not only make use of the default themes, but also modify these themes to suit your needs. You can add new theme elements or change the values of existing ones, and apply your changes globally or to a single plot.
9.5.4 See Also
The options for modifying themes are listed in Recipe 9.4.