Skip to content
R for the Rest of Us Logo

Customize Your Fonts

This lesson is locked

Get access to all lessons in this course.

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Heads up! Working with custom fonts changed significantly after I recorded the lessons for this course. I've added an updated video below. The new changes make working with custom fonts much easier than it used to be.

Your Turn

  1. Install the ragg and systemfonts package

  2. Change your settings in RStudio so you can render with AGG

  3. Change all text in your last plot to use a custom font (you’ll have to do this in a few different places)

  4. Save your plot using ggsave()

Learn More

If you’re interested in a quite technical blog post on the changes to working with custom fonts, Thomas Lin Pedersen, developer of ragg and systemfonts, has written up an explanation of the problem these packages solve.

The best resource for learning more about how to use custom fonts is this article by June Choe.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Abby Isaacson

Abby Isaacson

May 19, 2021

Just curious if the fonts library changes over time? For example, your Karla font isn't an option among the 158 font choices that show up for me.

David Keyes

David Keyes

May 20, 2021

The fonts library is just what you have installed on your system. Karla isn't an option for you because you haven't installed it. You can download it for free, though, if you want to use it.

Harold Stanislaw

Harold Stanislaw

May 26, 2021

This may be peculiar to my setup, but when I run the fonts command I see only 150 fonts, even though my system actually has quite a few more. (Perhaps they're not showing up because when I ran loadfonts I got this error message LOTS of times: "string 4 is invalid in this localeinput".) I tried using Bauhaus 93, which was not listed by the fonts command, and Bauhaus was actually applied to my graph. So, fonts are available to R that don't show up when running the fonts command.

Lucilla Piccari

Lucilla Piccari

May 26, 2021

Hello!

When I try to import "Karla" with the following code: font_import(pattern = "Karla") I get the following error message: Scanning ttf files in C:\Windows\Fonts ... Extracting .afm files from .ttf files... Error in data.frame(fontfile = ttfiles, FontName = "", stringsAsFactors = FALSE) : arguments imply differing number of rows: 0, 1

I have already imported other fonts and have verified that 187 are available, but Karla is not among them.

Am I doing something wrong?

Thank you!

Lucilla Piccari

Lucilla Piccari

May 26, 2021

Apologies, just realised this was already solved in the reply to Abby's question!

David Keyes

David Keyes

May 26, 2021

Did you download and install the Karla font? It won't work if you don't have the font installed locally.

Lucilla Piccari

Lucilla Piccari

June 5, 2021

Thank you, I will check it out!

I've tried three things and only Heiss' advice to specifically load Comic Sans works (which is not super helpful since i'll never use that font). I'm on Windows.

  1. I installed extrafont. When I run font_import() I get the following in the Console: > font_import() Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system. Continue? [y/n] y Scanning ttf files in C:\windows\Fonts ... Extracting .afm files from .ttf files... C:\Windows\Fonts\AGENCYB.TTF : No FontName. Skipping. C:\Windows\Fonts\AGENCYR.TTF : No FontName. Skipping. C:\Windows\Fonts\ALGER.TTF : No FontName. Skipping. C:\Windows\Fonts\ANTQUAB.TTF : No FontName. Skipping. .... C:\Windows\Fonts\WINGDNG3.TTF : No FontName. Skipping. C:\Windows\Fonts\ZWAdobeF.TTF : No FontName. Skipping. Found FontName for 0 fonts. Scanning afm files in C:/Users/mmakel1/OneDrive - Johns Hopkins/Documents/R/win-library/4.1/extrafontdb/metrics There were 50 or more warnings (use warnings() to see the first 50)"

  2. When I run windowsFonts() > windowsFonts() $serif [1] "TT Times New Roman" $sans [1] "TT Arial" $mono [1] "TT Courier New" >

  3. following Andrew Heiss' blog, when i run capabilities(), I do get TRUE for Cairo, but don't see it in my list of fonts.

  4. When I run, [from Heiss blog] windowsFonts(Comic Sans MS = windowsFont("Comic Sans MS") That increases my fonts to 4. (but not super helpful since I will never use Comic Sans). It's fine if I have to load them one at a time, but I don't know the official font names in Windows. ...The above errors aren't as the closest to Comic Sans MS are: C:\Windows\Fonts\comic.ttf : No FontName. Skipping. C:\Windows\Fonts\comicbd.ttf : No FontName. Skipping. C:\Windows\Fonts\comici.ttf : No FontName. Skipping.

David Keyes

David Keyes

December 7, 2021

Sorry, I thought I had updated this lesson to include the latest and greatest on custom fonts. I just updated the intro above the video on the lesson. Take a look at this video (I've also posted the code from it here) and let me know if it helps. The approach of using the systemfonts package, in theory, gets around the whole rigamarole that I show in this lesson. Let me know if it works for you!

If it makes you feel any better, I had trouble getting fonts to render on a project I was working on recently. As I say above, this is notoriously tricky in R.

I already had systemfonts and ragg packages. When i load those libraries and run windowsFonts(), it still returns with the same 3 fonts on my machine.

I tried font_import() again and it still returned: C:\Windows\Fonts\AGENCYB.TTF : No FontName. Skipping. C:\Windows\Fonts\AGENCYR.TTF : No FontName. Skipping. C:\Windows\Fonts\ALGER.TTF : No FontName. Skipping.

David Keyes

David Keyes

December 7, 2021

Can you run this and let me know what it returns:

library(systemfonts) match_font('Comic Sans')

Copy and pasting from this site inserted the wrong tick mark. I replaced with the apostrophe and got: > library(systemfonts) > match_font('Comic Sans') $path [1] "C:\windows\Fonts\arial.ttf" $index [1] 0 $features NULL

Sorry I wasn't clear.

In my first post, #4, I shared that I successfully loaded Comic Sans using code from Heiss' blog (that's how I submitted the Plots assignment). If that is what I have to do to load fonts one at a time, I can do that (once I find the list of official font names). But I've yet to figure out how to load a fonts package that will give me an option for fonts.

This page helped me add a different font. http://www.r-graph-gallery.com/custom-fonts-in-R-and-ggplot2.html

library(showtext) font_add_google("Special Elite", family = "special") windowsFonts(Special Elite = windowsFont("Special Elite"))

after running that code, windowsFonts() now shows that I have Special Elite as an available font.

Using that, I believe that I can access any font listed on https://fonts.google.com/ (which is a lot of fonts)

Oluwaseun Oyewole

Oluwaseun Oyewole

April 26, 2022

The issue is due to the package "Rttf2pt1". You have to downgrade it to a lower version (1.3.8) using the following steps:

library(extrafont) library(remotes) remotes::install_version("Rttf2pt1", version = "1.3.8") extrafont::font_import()

The Stackoverflow post that addresses this problem is here

Charlie Hadley

Charlie Hadley

April 28, 2022

It looks like the SO link got chopped off your comment. I believe you were linking to this answer.

Unfortunately, the {extrafont} package does depend on the {Rttf2pt1} package and it's clear from the documentation that {Rttf2pt1} is heading into obsolesce. I think that your method of using the old version is a good solution. An alternative approach could be to trying the {showtext} package instead.

JULIO VERA DE LEON

JULIO VERA DE LEON

May 6, 2022

I couldn't make it work. I installed the package, ran font import (I get a No Font Name error) and then with fonts I get a "NULL".

Charlie Hadley

Charlie Hadley

May 9, 2022

Hello Julio,

I believe this issue is connected to the {Rttf2pt1} package as discovered by Oluwaseun above and mentioned in this SO answer.

Unfortunately, while fonts are required by everyone who uses computers they are endlessly painful, frustrating and can be the source of security exploits. We're working to ensure our fonts advice is both as up to date and as flexible as possible.

In this instance I would try restarting R and trying this

library(extrafont)
library(remotes)
remotes::install_version(“Rttf2pt1”, version = “1.3.8”)
extrafont::font_import()

If this doesn't work then an alternative solution to investigate is {showtext}.

Cheers,

Charlie

JULIO VERA DE LEON

JULIO VERA DE LEON

May 10, 2022

Thank you Charlie.

Mychal Davis

Mychal Davis

June 2, 2022

Hello,

I am not able to load any fonts into R. I am using Windows. I tried using the code that was submitted below "library(extrafont) library(remotes) remotes::install_version(“Rttf2pt1”, version = “1.3.8”) extrafont::font_import()", but that did not work for me.

Also, I'm not certain that I am loading the fonts correctly in showtext. When I followed the directions listed on https://cran.rstudio.com/web/packages/showtext/vignettes/introduction.html, I got the following error: > showtext::font_add_google Error: 'font_add_google' is not an exported object from 'namespace:showtext'

David Keyes

David Keyes

June 3, 2022

Hi Mychal, can you please explain what happens when you run the extrafont code?

On the showtext code, I'm not sure why that's not working. Maybe reinstall the package and try again?

All of that said, your best bet is likely to be the approach I lay out in this video. Have you tried the approach with the ragg and systemfonts packages?

Sorry this is so difficult!

Hannah Ridenour LaFrance

Hannah Ridenour LaFrance

December 2, 2022

I'm getting an unused argument error on (base_family = "Ariel"), regardless of what font family I try. I had no problem viewing my list of available fonts, but none of them want to work with theme_minimal.

David Keyes

David Keyes

December 2, 2022

Are you putting the name of the font in quotes?

Hannah Ridenour LaFrance

Hannah Ridenour LaFrance

December 5, 2022

I am, as you do in the lesson video. I'll probably bring this to Charlie's office hours to try and tackle, if I don't resolve it by Tuesday.

David Keyes

David Keyes

December 5, 2022

Sounds good! Sorry I can't help more, but Charlie should be able to sort you out.