Themes

With the ‘theme’-argument (theme()) you can adjust (nearly) all appearence features of the graph. ggplot also provides some themes that can give you an impression of what is possible.

Standard themes

The standard themes of ggplot include: theme_bw, theme_gray, theme_dark, theme_classic, theme_minimal, theme_linedraw, theme_void

regular <- ggplot(mpg, aes(x = displ, y = cty, colour = drv)) + geom_point() +
  labs(title = "standard theme")
bw <- regular + theme_bw() + labs(title = "theme_bw")
gray <- regular + theme_gray() + labs(title = "theme_gray")
dark <- regular + theme_dark() + labs(title = "theme_dark")
classic <- regular + theme_classic() + labs(title = "theme_classic")
minimal <- regular + theme_minimal() + labs(title = "theme_minimal")
linedrw <- regular + theme_linedraw() + labs(title = "theme_linedraw")
void <- regular + theme_void() + labs(title = "theme_void")

Showing multiple figures at once takes some effort:

# install.packages("devtools")
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
regular + bw + gray + dark + classic + minimal + linedrw + void + plot_layout(ncol = 4)

Customized themes

You can really change a lot of features from the graph, allowing you to create the exact graph you had in mind. Look at all the options within theme (plus an explanation of each aspect here: http://ggplot2.tidyverse.org/reference/theme.html)

theme(line, rect, text, title, aspect.ratio, axis.title, axis.title.x,
  axis.title.x.top, axis.title.y, axis.title.y.right, axis.text, axis.text.x,
  axis.text.x.top, axis.text.y, axis.text.y.right, axis.ticks, axis.ticks.x,
  axis.ticks.y, axis.ticks.length, axis.line, axis.line.x, axis.line.y,
  legend.background, legend.margin, legend.spacing, legend.spacing.x,
  legend.spacing.y, legend.key, legend.key.size, legend.key.height,
  legend.key.width, legend.text, legend.text.align, legend.title,
  legend.title.align, legend.position, legend.direction, legend.justification,
  legend.box, legend.box.just, legend.box.margin, legend.box.background,
  legend.box.spacing, panel.background, panel.border, panel.spacing,
  panel.spacing.x, panel.spacing.y, panel.grid, panel.grid.major,
  panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x,
  panel.grid.minor.y, panel.ontop, plot.background, plot.title, plot.subtitle,
  plot.caption, plot.margin, strip.background, strip.placement, strip.text,
  strip.text.x, strip.text.y, strip.switch.pad.grid, strip.switch.pad.wrap, ...,
  complete = FALSE, validate = TRUE
)

Let’s change some features:

ggplot(mpg, aes(x = displ, y = cty, colour = drv)) + geom_point() +
  theme(
    axis.title = element_text(size = 18, face = "bold"),
    legend.position = "top",
    plot.background = element_rect("grey"),
    panel.background = element_rect("white"),
    panel.grid.major = element_line(size = 1, linetype = "dashed", colour = "darkgrey"),
    legend.background = element_rect(colour = "orange", fill = "green")
  )
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

What might be handy to realize is that you can also store your theme options and use them: Let’s change some features:

my_theme <- theme(
  axis.title = element_text(size = 18, face = "bold"),
  legend.position = "top",
  plot.background = element_rect("grey"),
  panel.background = element_rect("white"),
  panel.grid.major = element_line(size = 1, linetype = "dashed", colour = "darkgrey"),
  legend.background = element_rect(colour = "orange", fill = "green")
)

ggplot(mpg, aes(x = displ, y = cty, colour = drv)) + geom_point() + my_theme

Other themes

We can also use themes that were made by others. For instance, by making use of the package ggthemes we can recreate graphs in the style of “The economist”, or, “Wall Street Journal”, or old awful excel graphs. Let’s see some cool examples:

# install.packages("ggthemes")
library(ggthemes)
## 
## Attaching package: 'ggthemes'
## The following object is masked from 'package:cowplot':
## 
##     theme_map
regular <- ggplot(mpg, aes(x = displ, y = cty, colour = drv)) + geom_point() +
  labs(title = "standard theme")
econ <- regular + theme_economist() + labs(title = "theme_economist")
excel <- regular + theme_excel() + labs(title = "theme_excel")
fte <- regular + theme_fivethirtyeight() + labs(title = "theme_fivethirtyeight")
tufte <- regular + theme_tufte() + labs(title = "theme_tufte")
wsj <- regular + theme_wsj() + labs(title = "theme_wsj")

regular + econ + excel + fte + tufte + wsj + plot_layout(ncol = 3)

There are also more fun themse such as tv-themes:

# install.packages("tvthemes")
library(tvthemes)
# install.packages("extrafont")
library(extrafont)
## Registering fonts with R
regular +
  scale_colour_spongeBob() +
  theme_minimal() +
  labs(title = "Spongebob colours")

Or dark themes:

# install.packages("ggdark")
library(ggdark)

regular +
  dark_theme_gray() +
  labs(title = "dark theme")
invert_geom_defaults()