Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

Installing custom fonts in R

Add your own custom fonts to an installation of R on Windows or Linux.

If you would like to enhance the beauty of your plots in R, you can install your own fonts and have R use those instead of the system defaults.

Here are the methodologies to:

Install custom fonts in R on Linux

Step 1

In R, check your R library paths:

.libPaths()

Confirm the first path is somewhere under your home directory (e.g. ~/R/x86_64-pc-linux-gnu-library/4.x/). If not, create a personal library and add it to your ~/.Rprofile.

On the command line, enter the following:

mkdir -p ~/R/library

Then in R:

.libPaths(c("~/R/library", .libPaths()))

Step 2

In R, install extrafontdb and extrafont to your personal library:

install.packages(c("extrafontdb", "extrafont"), lib = .libPaths()[1])

Step 3

Using the command line (or SFTP if you prefer a GUI), install your font files onto the server (this is a one-time operation):

mkdir -p ~/.local/share/fonts
cp MyFont.ttf ~/.local/share/fonts/
fc-cache -fv

Step 4

In R, install your fonts:

library(extrafont)
font_import()

Step 5

Automate loading of your fonts for every R session via ~/.Rprofile:

if (require(extrafont)) {
  loadfonts()
}

Step 6

Generate plots!

Install custom fonts in R on Windows

Step 1

In R, check your R library paths:

.libPaths()

The first path should be something like c:/Users/YourName/AppData/Local/R/win-library/4.x/ unless you have set a custom location for your R library.

Step 2

In R, install extrafontdb and extrafont to your personal library:

install.packages(c("extrafontdb", "extrafont"))

Step 3

Windows fonts are stored at c:\Windows\Fonts\ and extrafont finds them automatically. In R, type the following commands:

library(extrafont)
font_import()

This can take a few minutes the first time. You only need to re-run font_import() if you install new fonts on your Windows machine.

Step 4

Automate loading of your fonts for every R session via .Rprofile. To open your file for editing, use the following command in R:

usethis::edit_r_profile()

Then add the following to your .Rprofile file:

if (require(extrafont)) {
  loadfonts(device="win")
}

Note the device = "win" argument: this is Windows-specific and registers fonts with the Windows graphics device.

Step 5

Generate plots!

   

Comments

No comments have yet been submitted. Be the first!

Have Your Say

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.