Home » A Complete Guide to the Best ggplot2 Themes

A Complete Guide to the Best ggplot2 Themes

by Erma Khan

This tutorial provides a complete guide to the best ggplot2 themes, including:

  • How to modify the appearance of plots using built-in ggplot2 themes.
  • How to modify the appearance of plots using predefined themes from the ggthemes library.
  • How to modify specific components of the theme including the plot panel background and the gridlines.

How to Modify Plot Appearance Using Built-in ggplot2 Themes

For each of the following examples, we’ll use the built-in R dataset iris:

#view first six rows of iris dataset
head(iris)

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

First, we’ll load the ggplot2 library and create a scatterplot that shows Sepal.Length on the x-axis and Sepal.Width on the y-axis, colored according to Species:

#load ggplot2 library
library(ggplot2)

#create scatterplot
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point()

ggplot2 scatterplot

Next, we’ll show how each of the built-in ggplot2 themes impact the appearance of the plot.

theme_gray

The default theme, featuring a gray background and white gridlines.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_gray()

ggplot2 scatterplot

theme_bw

A black on white theme.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_bw()

Ggplot2 scatter plot with theme_bw

theme_linedraw

A theme with only black lines of various widths on white backgrounds.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_linedraw()

Ggplot2 scatter plot with ggthemes

theme_light

A theme similar to theme_linedraw but with grey lines and axes designed to draw more attention to the data.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_light()

Ggplot2 theme example

theme_dark

A theme similar to theme_light, but with a dark background. A useful theme for making thin colored lines stand out.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_dark()

ggplot2 with dark theme

theme_minimal

A theme with no background annotations.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_minimal()

ggplot2 theme with theme_minimal

theme_classic

A theme with no gridlines.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_classic()

Classic theme in ggplot2

theme_void

A completely empty theme.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_void()

Empty theme in ggplot2

How to modify the appearance of plots using predefined themes from the ggthemes library

In addition to using the built-in ggplot2 themes, we can use the predefined themes from the ggthemes library to modify the aesthetics of plots.

First, we’ll load the ggthemes library:

library(ggthemes)

Next, we’ll show a few examples of how to use the predefined themes to modify the aesthetics of plots:

theme_wsj

A Wall Street Journal theme.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_wsj()

ggplot2 Wall Street Journal theme

theme_tufte

A minimalist theme inspired by the work of statistician Edward Tufte.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_tufte()

Ggplot theme inspired by Edward Tufte

theme_solarized

A theme that uses colors based on the solarized palette.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_solarized()

Solarized theme on ggplot2

Note that we can also use the argument light = FALSE to use a dark background on the plot:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_solarized(light = FALSE)

Ggplot2 example

theme_gdocs

A theme with Google Docs Chart defaults.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_gdocs()

Ggplot2 with google doc theme

theme_fivethirtyeight

Theme inspired by fivethirtyeight.com plots.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_fivethirtyeight()

Ggplot2 theme inspired by Five Thirty Eight

theme_economist

Theme inspired by The Economist.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_economist()

ggplot2 theme inspired by The Economist

How to Modify Specific Components of Plots

We can use the theme() and element_rect() functions to change the plot panel background color:

theme(panel.background = element_rect(fill, color, size))
  • fill: fill color for rectangle
  • color: border color
  • size: border size

We can also use the element_line() function to change the size and appearance of the gridlines:

theme(panel.grid.major = element_line(color, size, linetype),
      panel.grid.minor = element_line(color, size, linetype))
  • color: border color
  • size: border size
  • linetype: line type (“blank”, “solid”, “dashed”, “dotted”, “dotdash”, “longdash”, “twodash”)

The following code illustrates how to remove the plot panel borders and the gridlines:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme(panel.border = element_blank(),
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank())

ggplot example with no gridlines

The following code illustrates how to modify the plot panel background and the gridlines:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme(
    panel.background = element_rect(fill = "powderblue",
    color = "powderblue",
    size = 0.5, linetype = "solid"),
    panel.grid.major = element_line(size = 0.5, linetype = 'solid', color = "white"),
    panel.grid.minor = element_line(size = 0.25, linetype = 'solid', color = "white")
  )

Ggplot2 scatter plot example

Related Posts