9.5 Creating Your Own Themes

9.5.1 Problem

You want to create your own theme.

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
mytheme <- theme_bw() +
  theme(
    text = element_text(colour = "red"),
    axis.title = element_text(size = rel(1.25))
  )

# Create the base plot
hw_plot <- ggplot(heightweight, aes(x = ageYear, y = heightIn)) +
  geom_point()

# Plot with the modified theme we created
hw_plot +
  mytheme
A modified default theme

Figure 9.10: A modified default theme

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.