Adding Custom Fonts to ggplot in R

ggplot – You can spot one from a mile away, which is great! And when you do it’s a silent fist bump. But sometimes you want more than the standard theme.

Fonts can breathe new life into your plots, helping to match the theme of your presentation, poster or report. This is always a second thought for me and need to work out how to do it again, hence the post.

There are two main packages for managing fonts – extrafont, and showtext.

Extrafont

A relatively old package and it’s not well supported unfortunately. You can run into problems, however the base functions work well enough.

The fonts in the system directory are first imported into the extrafontdb using font_import(). This only needs to be run once in order to load the fonts into the right directory. Secondly, they are registered in R using loadfonts() for your specific device. The fonts need to be loaded each session.

Below are all the available fonts (that I have – click on the image to enlarge).

To use these fonts in a plot, change the text family using one of the names above. For demonstration I’ll use the Antigua corn data from the DAAG package.

It’s likely you’ll want more than what is available in the standard font set. You can add custom fonts with extrafont(), however I’ve only had limited success. A better option is using showtext.

Showtext

showtext is a package by Yixuan Qiu and it makes adding new fonts simple. There are a tonne of websites where you can download free fonts to suit pretty much any style you are going for. I’ll only touch on the key bits, so check out the vignette for more details.

The simplest way is to add fonts is via font_add_google(). Find the font you like on Google Fonts and add it to R using the following.

Amatic SC can now be used by changing the font family to “amatic-sc”. For R to know how to properly render the text we first need to run showtext_auto() prior to displaying the plot. One downside is it currently does not display in Rstudio. Either open a new graphics window with windows() or save as an external file e.g. .png.

Custom fonts are added by first,

  • Finding the font you want to use (I’ve picked a few from 1001 Free Fonts and Font Space, but there are many more out there)
  • Download the font .ttf() file and unzip if needed
  • Use font_add() to register the font
  • Run showtext_auto() to load the fonts

And that’s pretty much it. Given how effortless it is to add new fonts you can experiment with many different styles.

These fonts are outrageous but demonstrate that you really can go for any style, from something minimal and easy reading to something fit for a heavy metal band. For professional reports you’ll want to go for something sensible, but if you’re making a poster, website or infographic you may want to get creative e.g.

The tools are there for you to be as creative as you want to be.

The last thing to note is you’ll need to play around with different sizes given the resolution of your screen.

To turn off showtext and use the standard fonts, run

Code bits

Follow me on social media: