An Environment for Reliably Rendering Figures in R

[This article was first published on R | datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Fathom Data is working on a project to reproduce the figures from the CORE textbook The Economy using R and {ggplot2}. There’s a strict style guide which specifies the figure aesthetics including colours and font. We’re a team of seven people working on as many different setups. The principle challenges have been package versions and fonts.

We’ve solved the problem by creating a custom Docker image, which provides both packages and fonts. In retrospect, we should have done this much earlier.


This is the Dockerfile describing the image.

FROM rocker/verse:4.0.4

RUN apt-get update -qq && \
    apt-get install -y -qq ttf-ubuntu-font-family && \
    apt-get -y purge fonts-roboto fonts-roboto-unhinted fonts-lmodern fonts-texgyre && \
    # Clean up after apt.
    rm -rf /var/lib/apt/lists/*

# Install Google Fonts.
ARG GOOGLE_TTF=/usr/share/fonts/truetype/google-fonts
RUN git clone git:// && \
    mkdir -p $GOOGLE_TTF && \
    find fonts -regex '.*Asap-[a-zA-Z]+\.ttf$' -exec install -Dm644 {} $GOOGLE_TTF \; && \
    rm -rf fonts/ && \
    fc-cache -f > /dev/null

RUN install2.r -e \
      extrafont \
      ggnewscale \
      ggtext \
      googledrive \
      gridExtra \
      here \
      htmlwidgets \
      janitor \
      patchwork \
      plotly \
      readxl \
      rsvg \
      showtext \

RUN R -e "library(extrafont); font_import(prompt = FALSE); loadfonts(quiet = FALSE)"

COPY core ./core

RUN cd core && \
    R -e "devtools::build(); devtools::install()"


Here are the key points:

  • It’s based on the 4.0.4 version of the rocker/verse image, which means that it immediately includes the most recent version of R, RStudio, all of the {tidyverse} as well as TeX and other publishing-related packages.
  • All of the Google Fonts are cloned from their GitHub repository, but only Asap is retained.
  • The {extrafont} package is used to import system TTF fonts into R.
  • All of the other packages that we’re using are baked into the image.
  • The {core} package (a submodule on our repository), built and maintained by Megan Beckett, is copied across onto the image, built and installed.
  • A default password is set. There’s probably no harm in this since the image will only be used locally.


Create a volume to persist work between sessions.

docker volume create core

Run the image, mounting the volume and exposing port 8787.

docker run -it --rm \
    --name rcore \
    -p 8787:8787 \
    --mount source=core,target=/home/rstudio \

And access at


All of the data for the figures are stored in Google Sheets. So an important component of the workflow is authenticating with Google. We wanted this to occur immediately upon loading the {core} package and the authentication details to be persisted between sessions. Matt Dennis implemented this using the use_oob option to googledrive::drive_auth(), checking first for an interactive session to ensure that it wasn’t triggered during package build.

.onLoad <- function(libname, pkgname) {
  if (interactive()) {
    googledrive::drive_auth(use_oob = TRUE)

With this setup we have everything we need to create the figures in a stable, reproducible environment. No need to install fonts or packages. Rather than worrying about technical issues, we can focus on the figures.

???? Thanks to the maintainers of the version-stable Rocker images, which made putting this together a lot easier.

To leave a comment for the author, please follow the link and comment on their blog: R | datawookie. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)