Our Corporate D6 ggplot2 Theme

rstats ggplot2 workflow tutorial

Learn how to use the theme_d6() featured in our {d6} package to create figures that match our lab identity. The theme function comes with a set of helpful additional options to simplify customization of our corporate D6 theme while ensuring consistency.

Cedric Scherer https://cedricscherer.com (IZW Berlin)https://ecodynizw.github.io/
2023-11-30

Recently, I added a custom theme for {ggplot2} to our {d6} workflow package which makes it easy to create plots in a style that matches our lab identity used for our webpage, posters, and publications. Also, it hopefully simplifies your workflow by a ready-to-use theme with additional options that allow you to adjust the style of your ggplots.

Let us know if there is a theme feature you often modify via the theme() function! We can add it as an argument to the complete theme function.

## install (if needed) and load packages
d6::simple_load(c("ggplot2", "EcoDynIZW/d6"))

The default theme features a vertical and horizontal grid, our corporate sans font, and a transparent background. It also uses a larger default base_size of 14 pts that controls the size of text labels as well as line elements and borders (the ggplot2 default is 11 pts).

## prepare data
data <- dplyr::filter(economics_long, !variable %in% c("pop", "pce"))

## create base plot
g <- 
  ggplot(data, aes(x = date, y = value01, color = variable)) +
  geom_line()

## apply corporate theme
g + d6::theme_d6()

As usual with complete themes, you can overwrite the base settings, namely base_family, base_size, base_size_line, and base_size_rect:

## modify theme base setting
g + d6::theme_d6(base_size = 18, base_rect_size = 2)

Typeface Choices

Main Typeface

The default typeface used is PT Sans. If you prefer a serif typeface, you can switch to PT Serif by setting serif = TRUE.

g + d6::theme_d6(serif = FALSE) ## uses PT Sans (default)
g + d6::theme_d6(serif = TRUE)  ## uses PT Serif

Note that the typefaces need to be installed on your machine. A warning will inform you if the relevant fonts are not installed.

Please use the {systemfonts} package to access font files as this is the most recent and best implementation to use non-default fonts in combination with {ggplot2}. Loading other font packages, especially the {showtext} package, likely cause problems.

Tabular Font Option

For numeric axis labels and legends, you might want to use a tabular typeface (i.e. one where the characters all have the same width). You can set the axis and legend text individually by combining x, y, and l (uppercase letters work as well).

## all combinations of upper- and lowercase work
g + d6::theme_d6(mono = "xyl")
g + d6::theme_d6(mono = "xy")
g + d6::theme_d6(mono = "y")

Legend Position

By default, the legend is placed at the bottom. You can easily change the position via the legend argument. This is a shortcut to theme(legend.position) and thus you can pass either strings specifying the position or a vector defining the x and y position as usual:

g + d6::theme_d6(legend = "right")
g + d6::theme_d6(legend = "none")
g + d6::theme_d6(legend = c(.65, .8))

Control Grid

The default themes comes with no grid lines but you can easily add them by specifying “x” for vertical, “y” for horizontal, or “xy” for both, vertical and horizontal grid lines (uppercase letters work as well).

All theme styles do not feature minor grid lines to avoid cluttering and distractions.

g + d6::theme_d6(grid = "none") ## "" works as well (default)
g + d6::theme_d6(grid = "xy") ## "XY" works as well, and even "xY" or "Xy"
g + d6::theme_d6(grid = "x") ## "x" works as well
g + d6::theme_d6(grid = "y") ## "Y" works as well

Modify Background Color

By default the background color of all boxes (plot, panel, and legends) is transparent. You can adjust the colors by passing any color name or hex code to the bg argument:

g + d6::theme_d6(bg = "orange")
g + d6::theme_d6(bg = "grey95")
g + d6::theme_d6(bg = "beige")

It is not possible to control individual boxes via theme_d6(). If you wish to, for example, color the background of the panel area in a different, you have to specify that via the theme() function as usual:

g + d6::theme_d6(bg = "orange") + 
  theme(panel.background = element_rect(fill = "white", color = "transparent"))

Set Plot Margin

Depending on the use case, you might want to adjust the margin around the plot. The default is related to the base_size and the same for all sides (top, right, bottom, left) specified as rep(base_size / 2, 4). You can modify the margins easily within the theme_d6().

g + d6::theme_d6(margin = rep(0, 4), bg = "grey95")           ## no margins
g + d6::theme_d6(margin = rep(50, 4), bg = "grey95")          ## same margins
g + d6::theme_d6(margin = c(120, 40, 40, 10), bg = "grey95")  ## different margins

Citation

For attribution, please cite this work as

Scherer (2023, Nov. 30). Ecological Dynamics: Our Corporate D6 ggplot2 Theme. Retrieved from https://ecodynizw.github.io/posts/d6-ggplot-theme/

BibTeX citation

@misc{scherer2023our,
  author = {Scherer, Cedric},
  title = {Ecological Dynamics: Our Corporate D6 ggplot2 Theme},
  url = {https://ecodynizw.github.io/posts/d6-ggplot-theme/},
  year = {2023}
}