Chapter 9 Controlling the Overall Appearance of Graphs

In this chapter I’ll discuss how to control the overall appearance of graphics made by ggplot2. The grammar of graphics that underlies ggplot2 is concerned with how data is processed and displayed – it’s not concerned with things like fonts, background colors, and so on. When it comes to presenting your data, there’s a good chance that you’ll want to tune the appearance of these things. ggplot2’s theming system provides control over the appearance of non-data elements. I touched on the theme system in the previous chapter, and here I’ll explain a bit more about how it works.

9.1 Setting the Title of a Graph

9.1.1 Problem

You want to add a title to your plot.

9.1.3 Discussion

ggtitle() is equivalent to using labs(title = "Title text").

You can add a subtitle by providing a string as the second argument of ggtitle(). By default it will display with slightly smaller text than the main title.

Scatter plot with a subtitle

Figure 9.2: Scatter plot with a subtitle

If you want to move the title inside the plotting area, you can use one of two methods, both of which are a little bit of a hack (Figure 9.3). The first method is to use ggtitle() with a negative vjust value. The drawback of this method is that it still reserves blank space above the plotting region for the title.

The second method is to instead use a text annotation, setting its x position to the middle of the x range and its y position to Inf, which places it at the top of the plotting region. This also requires a positive vjust value to bring the text fully inside the plotting region:

Title with ggtitle and a negative vjust value (note the extra space above the plotting area) (left); With a text annotation at the top of the figure (right)Title with ggtitle and a negative vjust value (note the extra space above the plotting area) (left); With a text annotation at the top of the figure (right)

Figure 9.3: Title with ggtitle and a negative vjust value (note the extra space above the plotting area) (left); With a text annotation at the top of the figure (right)

9.2 Changing the Appearance of Text

9.2.1 Problem

You want to change the appearance of text in a plot.

9.2.3 Discussion

There are two kinds of text items in ggplot2: theme elements and text geoms. Theme elements are all the non-data elements in the plot: the title, legends, and axes. Text geoms are things that are part of the plot itself, and reflect the data content.

There are differences in the parameters, as shown in Table 9.1.

Table 9.1: Text properties of theme elements and text geoms
Theme elements Text geoms Description
family family Helvetica, Times, Courier
face fontface plain, bold, italic, bold.italic
colour colour Color (name or “#RRGGBB”)
size size Font size (in points for theme elements; in mm for geoms)
hjust hjust Horizontal alignment: 0 = left, 0.5 = center, 1 = right
vjust vjust Vertical alignment: 0 = bottom, 0.5 = middle, 1 = top
angle angle Angle in degrees
lineheight lineheight Line spacing multiplier

The theme elements are listed in Table 9.2. Most of them are straightforward. Some are shown in Figure 9.6.

Table 9.2: Theme items that control text appearance in theme()
Element name Description
axis.title Appearance of axis labels on both axes
axis.title.x Appearance of x-axis label
axis.title.y Appearance of y-axis label
axis.ticks Appearance of tick labels on both axes
axis.ticks.x Appearance of x tick labels
axis.ticks.y Appearance of y tick labels
legend.title Appearance of legend title
legend.text Appearance of legend items
plot.title Appearance of overall plot title
strip.text Appearance of facet labels in both directions
strip.text.x Appearance of horizontal facet labels
strip.text.y Appearance of vertical facet labels
Aligning with hjust and vjust, and spacing with lineheight

Figure 9.6: Aligning with hjust and vjust, and spacing with lineheight

9.3 Using Themes

9.3.1 Problem

You want to use premade themes to control the overall plot appearance.

9.3.2 Solution

There are many premade themes that are already included in ggplot2. The default ggplot2 theme is theme_grey(), but the examples below also showcase theme_bw(), theme_minimal(), and theme_classic().

To use a premade theme, add theme_bw() or another theme to your plot (Figure 9.7):

Scatter plot with theme_grey() (the default, top left); with theme_bw() (top right); with theme_minimal() (bottom left); with theme_classic() (bottom right)Scatter plot with theme_grey() (the default, top left); with theme_bw() (top right); with theme_minimal() (bottom left); with theme_classic() (bottom right)Scatter plot with theme_grey() (the default, top left); with theme_bw() (top right); with theme_minimal() (bottom left); with theme_classic() (bottom right)Scatter plot with theme_grey() (the default, top left); with theme_bw() (top right); with theme_minimal() (bottom left); with theme_classic() (bottom right)

Figure 9.7: Scatter plot with theme_grey() (the default, top left); with theme_bw() (top right); with theme_minimal() (bottom left); with theme_classic() (bottom right)

Another theme included in ggplot2 is theme_void(), which makes all plot elements blank and only shows your data (Figure 9.8. This is especially useful if you don’t want any default theme settings, and instead want a blank slate on which to choose your own theme elements.

Scatter plot with theme_void()

Figure 9.8: Scatter plot with theme_void()

9.3.3 Discussion

Some commonly used properties of theme elements in ggplot2 are those things that are controlled by theme(). Most of these things, like the title, legend, and axes, are outside the plot area, but some of them are inside the plot area, such as grid lines and the background coloring.

Besides the themes included in ggplot2, it is also possible to create your own.

You can set the base font family and size with either of the included themes (the default base font family is Helvetica, and the default size is 12):

You can set the default theme for the current R session with theme_set(), although it’s generally not a good idea to set options globally because it may affect other plots that are unrelated to your current project:

9.3.4 See Also

To see additional complete themes included in ggplot2, see https://ggplot2.tidyverse.org/reference/ggtheme.html.

To modify a theme, see Recipe 9.4.

To create your own themes, see Recipe 9.5.

See ?theme to see all the available theme properties.

9.4 Changing the Appearance of Theme Elements

9.4.1 Problem

You want to change the appearance of theme elements.

9.4.2 Solution

To modify a theme, add theme() with a corresponding element_xx object. These include element_line, element_rect, and element_text. The following code shows how to modify many of the commonly used theme properties (Figure 9.9):

Clockwise from top left: modifying theme properties for the plotting area, the legend, the facets, and the text itemsClockwise from top left: modifying theme properties for the plotting area, the legend, the facets, and the text itemsClockwise from top left: modifying theme properties for the plotting area, the legend, the facets, and the text itemsClockwise from top left: modifying theme properties for the plotting area, the legend, the facets, and the text items

Figure 9.9: Clockwise from top left: modifying theme properties for the plotting area, the legend, the facets, and the text items

9.4.3 Discussion

If you want to use a saved theme and tweak a few parts of it with theme(), the theme() must come after the theme specification. Otherwise, anything set by theme() will be unset by the theme you add:

Many of the commonly used theme properties are shown in Table 9.3.

Table 9.3: Theme items that control text appearance in theme()
Name Description Element type
text All text elements element_text()
rect All rectangular elements element_rect()
line All line elements element_line()
axis.line Lines along axes element_line()
axis.title Appearance of both axis labels element_text()
axis.title.x X-axis label appearance element_text()
axis.title.y Y-axis label appearance element_text()
axis.text Appearance of tick labels on both axes element_text()
axis.text.x X-axis tick label appearance element_text()
axis.text.y Y-axis tick label appearance element_text()
legend.background Background of legend element_rect()
legend.text Legend item appearance element_text()
legend.title Legend title appearance element_text()
legend.position Position of the legend "left", "right", "bottom", "top", or two-element numeric vector if you wish to place it inside the plot area (for more on legend placement, see Recipe 10.2)
panel.background Background of plotting area element_rect()
panel.border Border around plotting area element_rect(linetype="dashed")
panel.grid.major Major grid lines element_line()
panel.grid.major.x Major grid lines, vertical element_line()
panel.grid.major.y Major grid lines, horizontal element_line()
panel.grid.minor Minor grid lines element_line()
panel.grid.minor.x Minor grid lines, vertical element_line()
panel.grid.minor.y Minor grid lines, horizontal element_line()
plot.background Background of the entire plot element_rect(fill = "white", colour = NA)
plot.title Title text appearance element_text()
strip.background Background of facet labels element_rect()
strip.text Text appearance for vertical and horizontal facet labels element_text()
strip.text.x Text appearance for horizontal facet labels element_text()
strip.text.y Text appearance for vertical facet labels element_text()

9.5 Creating Your Own Themes

9.5.1 Problem

You want to create your own 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.

9.6 Hiding Grid Lines

9.6.1 Problem

You want to hide the grid lines in a plot.

9.6.2 Solution

The major grid lines (those that align with the tick marks) are controlled with panel.grid.major. The minor grid lines (the ones between the major lines) are controlled with panel.grid.minor. This will hide them both, as shown in Figure 9.11 (left):

9.6.3 Discussion

It’s possible to hide just the vertical or horizontal grid lines, as shown in the middle and right-hand graphs in Figure 9.11, with panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, and panel.grid.minor.y:

No grid lines (left); No vertical lines (middle); No horizontal lines (right)No grid lines (left); No vertical lines (middle); No horizontal lines (right)No grid lines (left); No vertical lines (middle); No horizontal lines (right)

Figure 9.11: No grid lines (left); No vertical lines (middle); No horizontal lines (right)