16  Themes

16.1 Introduction

NoteWhy this chart

Themes take a correct plot to publication-ready: fonts, gridlines and background tuned once, reused everywhere.

WarningCommon pitfall

Styling before the data mapping is right wastes effort; get geoms and scales correct first.

In this final chapter, we will learn to modify the appearance of all non data components of the plot such as:

  • axis
  • legend
  • panel
  • plot area
  • background
  • margin
  • facets

16.2 Basic Plot

We will continue with the scatter plot examining the relationship between displacement and miles per gallon from the the mtcars data set.

p <- ggplot(mtcars) +
  geom_point(aes(disp, mpg))
p

16.3 Style plot axes

16.3.1 Style axis text

The axis.title.x argument can be used to modify the appearance of the X axis. In the below example, we modify the color and size of the title using the element_text() function. Remember, whenever you are trying to modify the appearance of a theme element which is a text, you must use element_text().

You can use axis.title.y to modify the Y axis title and to modify the title of both the axis together, use axis.title.

p + theme(axis.title.x = element_text(color = "red", size = 10, face = "italic"))

16.3.2 Style axis ticks

To modify the appearance of the axis ticks, use the axis.ticks argument. You can change the color, linewidth, linetype and length of the ticks using the element_line() function as shown below.

p + theme(axis.ticks = element_line(color = 'blue', linewidth = 1.25, linetype = 2), 
          axis.ticks.length = unit(1, "cm"))

16.3.3 Style axis lines

The axis.line argument should be used to modify the appearance of the axis lines. You can change the color, linewidth and linetype of the line using the element_line() function.

p + theme(axis.line = element_line(color = 'red', linewidth = 1.5, linetype = 3))

16.4 Style the legend

Now, let us look at modifying the non-data components of a legend.

p <- ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = factor(cyl), shape = factor(gear)))
p

16.4.1 Style the legend background

The background of the legend can be modified using the legend.background argument. You can change the background color, the border color and line type using element_rect().

p + theme(legend.background = element_rect(fill = 'gray', linetype = 3,  
          color = "black"))

16.4.2 Style legend text

The appearance of the text can be modified using the legend.text argument. You can change the color, size and font using the element_text() function.

p + theme(legend.text = element_text(color = 'green', face = 'italic'))

16.4.3 Style the legend title

The appearance of the title of the legend can be modified using the legend.title argument. You can change the color, size, font and alignment using element_text().

p + theme(legend.title = element_text(color = 'blue', face = 'bold', hjust = 0.1))

16.4.4 Move the legend position

The position and direction of the legend can be changed using:

  • legend.position
  • and legend.direction
p + theme(legend.position = "top", legend.direction = "horizontal")

16.5 Apply a complete theme

16.5.1 Classic Dark on Light

ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  theme_bw()

16.5.2 Default Gray

ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  theme_gray()

16.5.3 Light

ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  theme_light()

16.5.4 Minimal

ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  theme_minimal()

16.5.5 Dark

ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  theme_dark()

16.5.6 Classic

ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  theme_classic()

16.5.7 Void (Empty)

ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  theme_void()

16.6 Exercises

  1. Apply theme_minimal() to a scatter plot.
  2. Enlarge all text with theme(text = element_text(size = 14)).
  3. Build a reusable custom theme and explain what it standardizes.

Worked solutions in solutions/themes.md (attempt first).

Where to go next

  • ggplot2 book (ggplot2-book.org) for theory behind this layer
  • Claus Wilke, Fundamentals of Data Visualization, for perception guidance