The aim of this project is to optimise a workflow for designing and testing custom ggplot themes in R.
This project is for you if you are developing ggplot2 themes, or trying to figure out how a theme looks on a variety of plots (or a variety of themes on a plot). This project is built on the purrr
workflow from this blog post.
Sample output from theme_executive()
:
You don't need much to get started:
- {tidyverse} (essential)
- {patchwork} (essential)
- RStudio (recommended)
- {extrafont} (recommended)
{extrafont} enables you to use a much wider range of fonts in your ggplots. If you have never initialised {extrafont} before, I recommend you to run the following lines of code. You'll only need to do this once:
install.packages("extrafont")
extrafont::font_import()
extrafont::loadfonts(device = "win")
This process of loading the fonts can take around 5 minutes, but I assure you it is well worth it.1 After the process is completed, you will just need to load the {extrafont} library with library(extrafont)
to use the desired fonts in ggplot.
You can get started by cloning this repository, using GitHub Desktop or cloning it the traditional way using git. Open the .Rproj
file in the main directory with RStudio, and open/save your scripts in the scripts
folder (Don't use setwd()
!).2
-
Load {tidyverse} and {extrafont} to ensure all ggplot2 features and fonts are available in the design and testing process.
-
Use
source()
to load in the pre-built ggplot objects from thebase_plots
folder. These are basically simple plots created with {ggplot2} to be used for designing and testing the themes you are creating. You can create more complicated base ggplots for testing if you prefer. -
Create your ggplot theme as a function, and save the R file under "themes". In the main script, use
source()
again to load in the theme you've created. -
Run the test function to iterate through how your theme would look like with different base plot configurations. In this workflow, I use {patchwork} to combine the plots for easy viewing.
Here is an example using the theme_dbz()
theme that I used on the plots from my blog post Data cleaning with Kamehamehas in R:
library(tidyverse)
library(patchwork)
library(extrafont)
#### Load base plots ####
source("base_plots/base_plots.R")
#### Design theme ####
source("themes/theme_dbz.R")
#### test themes ####
test_themes <- function(plots,
theme,
theme_label){
plots +
theme +
labs(subtitle = theme_label)
}
#### Run plots ####
base_plots %>%
map(test_themes,
theme = theme_dbz(),
theme_label = "theme: theme_dbz()") %>%
reduce(`+`)
And here is the result:
I am using this project for my personal use in developing themes. Please feel free to explore the themes that I am working on:
Apart from themes, another feature that is being incorporated into this workflow is the ability to develop colour palettes. Currently, there is a brands_pal.R
file that allows you to use the colours of some well-known brands:
You can also apply this workflow to themes in existing packages. Here is the Economist theme from {ggthemes}
This repository now includes 8 additional custom themes with diverse styles:
- Minimal Dark - Clean, modern dark theme
- Retro - 80s/90s cyberpunk inspired
- Academic - Professional publication style
- Neon - Cyberpunk with bright accents
- Nature - Earth tones and organic feeling
- Corporate - Modern business presentation style
- Vintage - Classic newspaper/magazine design
- Tech - Modern technology company design
📖 Full documentation with examples: docs/NEW_THEMES_DOCUMENTATION.md
1. For more information on the installation process, please see this Stack Overflow thread: https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2
2 See https://martinctc.github.io/blog/rstudio-projects-and-working-directories-a-beginner's-guide/.