Custom ggplot2 themes

1. Custom ggplot2 themes

In the last set of exercises you created a very effective visualization that shows the relationship between two variables - the scatter plot. Now, let's make it more special. And this is where custom ggplot2 themes come in handy.

2. The advantages of a custom look

Take a look at both histograms shown here. The left one is made with the default ggplot2 look, while the right one has custom fonts and colors. I would argue there are a couple of advantages that come with such custom styles. First of all, you can use colors to better highlight certain aspects of your plot. Here the grid lines and the background are less dominant, while the bars of the histogram appear to be more present and thus important. Secondly, with a custom theme, you give your plot your own style and branding. Nowadays, everybody recognizes the default ggplot2 look, which is great, but might look cheap in the eyes of some experienced R users. With a custom look you communicate a sort of speciality or even exclusiveness, which has a subtle effect on how your plot and your results are perceived. So, let's have a look at how themes work.

3. The theme() function

The theme function is added to the plot object like geometries or other functions like labs. Within the theme function, you can alter components or elements of a theme, for instance all text elements, like in this example. In order to do that, you need to provide the appropriate argument with a function call from the element function family. In this case, element_text. Within this function call, you can specify a wide range of arguments that steer the look of this element. Here, we used the family and color argument to change all text elements in the plot to a gray serif font. Here, this affects the axes titles of the plot.

4. Default ggplot2 themes

ggplot2 also comes with a variety of default themes that you can just apply to your plot. They'll change a lot of components at the same time. In the classic theme for example, grid lines are removed and the axes are more pronounced, just to name a few.

5. Chaining theme() calls

Another cool thing about themes is that they can be combined and chained together. For instance, we could apply the classic theme, but then overwrite some of its components. Here, we just added another theme call to overwrite the style of the text elements in the plot.

6. Theme configuration options

Custom themes allow you to change the style of basically all elements of your plot. You can get an overview of all configuration options by calling the documentation of the theme function. Theme options are very fine-grained, as this excerpt from the documentation shows. For instance, you can change the appearance of only the x-axis, while leaving the y-axis as it is. The documentation also shows which function from the element function family to use for each configuration option.

7. The element_* function family

As said, theme configuration options are specified with functions from the element function family. There are four of these: element_text, _rect, and _line for text, rectangular and line elements, respectively, and there is element_blank. This last one is a bit special, as it is used to make plot elements disappear. One could specify element_blank for all text elements, for instance, which would remove the axes labels and titles in our histogram.

8. Let's try out themes!

Time to put this into practice. Let's try out some themes.