Using Custom Fonts in R

How to install a font and use it in ggplot.

For my new survival analysis project on baseball career longevity, I am using the TR Plaza font adopted by the Cleveland Guardians for the 2022 season. Style choices shouldn’t be gratuitous, but I feel like using the Guardians font for my graphics connects statistical analysis to the underlying topic of baseball. Anyway, it’s cool, so I’m doing it.

Before

After

I ran into difficulties adding the font, so I’m capturing the process here to smooth the way for you and for future me. These instructions explain how to change the font face used in your R graphics. It does not change the text output.

The standard way

If you already have the font installed on you system (i.e., it’s an available font in Microsoft Word), then you can follow the steps in this tutorial by R Coder. Here is an overview.

The extrafont package (GitHub) makes system fonts available for bitmap output. It maintains a database of the font metadata. Install the package. The installation will automatically bring in two other utility packages, extrafontdb and Rttf2p1.

install.packages("extrafont")

You only have to create the database of fonts on your system once. After that, it will be available to you whenever you load the extrafont package in your session. Create the database with font_import().

library(extrafont)
font_import()

This took my laptop about 15 minutes to run. Once its done, you can include your fonts in your ggplots like this.

library(tidyverse)
library(extrafont)
## Registering fonts with R
# Extrafont documentation has you load the fonts, but I accidentally left this
# out and it worked anyway. When you run the `library()` statement it outputs a
# message that it is "Registering fonts with R."
# loadfonts(device="win")

ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point() +
  ggtitle("TR Plaza font!") +
  theme(text = element_text(size = 16, family="TR Plaza"))

The key is setting the family argument of the element_text() function. How do you know what string to use? The name that appears in your MS Word font selection seems to always work. You can see the registered fonts with extrafont::fonts(), or even more detail with extrafont::fonttable().

fonts() %>% .[1:6] # just the first 6
## [1] "Agency FB"             "Algerian"              "Arial Black"          
## [4] "Arial"                 "Arial Narrow"          "Arial Rounded MT Bold"
fonttable() %>% head()
##   package        afmfile                        fontfile          FullName
## 1      NA AGENCYB.afm.gz C:\\Windows\\Fonts\\AGENCYB.TTF    Agency FB Bold
## 2      NA AGENCYR.afm.gz C:\\Windows\\Fonts\\AGENCYR.TTF         Agency FB
## 3      NA   ALGER.afm.gz   C:\\Windows\\Fonts\\ALGER.TTF          Algerian
## 4      NA  ariblk.afm.gz  C:\\Windows\\Fonts\\ariblk.ttf       Arial Black
## 5      NA arialbi.afm.gz C:\\Windows\\Fonts\\arialbi.ttf Arial Bold Italic
## 6      NA arialbd.afm.gz C:\\Windows\\Fonts\\arialbd.ttf        Arial Bold
##    FamilyName           FontName  Bold Italic Symbol afmsymfile
## 1   Agency FB      AgencyFB-Bold  TRUE  FALSE  FALSE         NA
## 2   Agency FB       AgencyFB-Reg FALSE  FALSE  FALSE         NA
## 3    Algerian           Algerian FALSE  FALSE  FALSE         NA
## 4 Arial Black        Arial-Black FALSE  FALSE  FALSE         NA
## 5       Arial Arial-BoldItalicMT  TRUE   TRUE  FALSE         NA
## 6       Arial       Arial-BoldMT  TRUE  FALSE  FALSE         NA

What if you don’t already have the font?

Hopefully that is all you need inorder to customize your graphics. But what if you don’t already have the font installed on your system? TR Plaza wasn’t already installed on mine. Here’s how I added it. I googled “install TR Plaza font” and one of this hits was Cufon Fonts. I clicked the download link. The site returned a zip file to my Downloads directory with the True Type Font file inside.

Double-clicking the .ttf file in the zip file opened a preview and installation app. I clicked Install to install the font.

That did it. Moving over to Microsoft Word, there’s my new font.

Returning to the tutorial, it says you can import additional fonts by supplying the path. Where is your new font? On Windows, you can find it like this. Open your system settings and go to Personalization > Fonts. Find your font and click it.

Use the font file path in the import function.

font_import(path = "C:/Users/mpfol/AppData/Local/Microsoft/Windows/Fonts")

If that doesn’t work

When I ran font_import() I got the following error.

font_import(path = "C:/Users/mpfol/AppData/Local/Microsoft/Windows/Fonts")

I don’t know why this error occurs, but I did discover how to fix it. This thread at Stack Overflow suggests installing version 1.3.8 of the Rttf2pt1 package.

remotes::install_version("Rttf2pt1", version = "1.3.8")

This time the command worked.

extrafont::font_import(path = "C:/Users/mpfol/AppData/Local/Microsoft/Windows/Fonts")

That’s all there is to it. The package GitHub page has more instructions, especially for embedding fonts into pdf files.

 Share!

 
comments powered by Disqus