Themes from scratch

1. Themes from scratch

The themes layer controls all the non-data ink on your plot.

2. The themes layer

Which are all the visual elements that are not actually part of the data.

3. The themes layer

Visual elements can be classified as one of three different types - text, line or rectangle.

4. The themes layer

Each type can be modified by using the appropriate function, which all begin with element_ followed by text, line, or rect, respectively.

5. A starting plot...

For example, consider this plot that we've already encounter a few times. It's composed out of a combination of data and lots of non-data embellishments. For example, these are all the text elements on our plot

6. The text elements

Each element has it's own unique name. We can access all the text in general, all titles in general but also more and more specific items, such as the plot, legend and axis titles, or the text on specific axes. All these items are arguments of the theme function.

7. The text elements

This is used like all other layers in ggplot, by adding a plus to our plot.

8. Adjusting theme elements

To modify an element, just call its argument in the theme function and use the appropriate element_ function to specify what we want to change, in this case we need to use an element_text call. It's within this function that we manipulate things like size, color, alignment and angle of the text.

9. A starting plot...

Lines include the tick marks on the axes, the axis lines themselves and all grid lines, both major and minor.

10. Line elements

These are also all just arguments within the theme function and are modified by the element_line argument.

11. A starting plot...

The remaining non-data ink on our plot are all rectangles of various sizes.

12. Rect elements

Access rectangles using arguments in the theme function and modify them using element_rect.

13. Hierarchical naming reflects inheritance rules

Although we have access to every item, we don't need to modify them individually. They inherent from each other in a hierarchy. All text elements inherit from text, so if we changed that argument, all downstream arguments would be affected. The same goes for line and rectangle. The naming convention is pretty intuitive. In practice you will call a small combination of arguments that you want to change.

14. element_blank()

There is one other element function that we haven't discussed yet: element_blank. We can use this in a plot to remove any item. That is, it won't be drawn at all. In this example we set all lines, text and rectangles to blank, so we are left with just the data. Notice that the legend keys themselves are part of the data, if you want to modify these elements refer back to the chapter aesthetics and the exercises on scales.

15. Let's practice!

Here, we saw how the theme layer allows us to modify visual attributes of non-data ink, including text lines and rectangles. But in addition to that, we can also modify a plot's margins and legend position. Let's head over to the exercises and see how this in action.