Building Stories With Data - Getting fonts to work in R (2024)

dataviz

typography

how-to

Getting fonts to work in R and RStudio can be tricky! This post walks through the different steps we need to follow to give ourselves the best chance of success.

Published

January 12, 2024

I’ve talked about choosing fonts and optimising text hierarchy in a several workshops recently, but in crafting documentation for a client whose visualisations require a font which isn’t typically installed as a default on a user’s computer, I found myself retracing the steps I’ve taken in my projects to get new fonts to work. For me, 2023 was the year of finally getting fonts to work smoothly 99% of the time in R (there’s always an exception!), so I thought I’d share this step-by-step guide more widely, in the hope of saving others a lot of the font frustration I experienced in getting to this point.

Let’s go!

Choose a font

I won’t go into detail on what makes a good dataviz font here, because I’ve talked about that elsewhere and this article is about the mechanics of getting a chosen font into your visualisations, but here are a quick few tips to get you going:

  • Make sure it reads well when the letters are small (try squinting at the letters ceo - do they look distinct enough?)
  • Make sure it is neither too wide (takes too long to look across all the information you need to see at once) nor too narrow (everything looks a bit cramped together and you’ll struggle with the ceo task)
  • Make sure the font has additional readability features such as qp and db not being perfect mirror images, and I1l (capital i, number 1, small l) are distinct from each other
  • Make sure it looks on-brand for you! Whether that’s super serious, super quirky, or somewhere in between, fonts are an easy way to add a bit of personality to your visualisations.

For this post, I’m using the Karla font family. It has a modern feel, and the letters are nicely spaced out which makes it a good fit for small text in dataviz and tables.

Check {systemfonts} is installed

The world of fonts in R has changed quite substantially over the last few years. The current received wisdom is to use {systemfonts}. So that’s step 1. An easy way to check it is installed is to try to use it. Type:

 systemfonts::system_fonts()

This should bring up with a tibble in your console output showing you which fonts are installed on your device. If that didn’t work, or you got a message saying there is no package called ‘systemfonts’, then you need to install it:

install.packages("systemfonts")

It will ask you about dependencies. Say yes. You need to install the packages that {systemfonts} depends on.

Check the font you want to use is installed

Now that we’re sure {systemfonts} is installed, we can use it to check we have the font we need. Type:

 systemfonts::system_fonts() |> View()

This should bring up a dataframe for you to look at. Use the search bar to look for your font (in this case, we’re looking for “Karla”).

If you don’t find the font there, you need to install it. If it’s a google font, navigate to Google Fonts, search for the font, select it to go to its own page, and click on “Download family”. Once it has downloaded, unzip the file, select all the .ttf files and, if you’re on a Windows device, right click and select “Install for all users”. If you’re on a mac, follow this guide. R tends to play nicer with .ttf fonts than other types of fonts. There are ways round that, but things will be easier if you choose a static font that has .ttf files!

Restart RStudio, and try to search for your font again, as we did above using systemfonts::system_fonts() |> View(). It should now be there! The dataframe gives us additional information about font properties such as weight, width and style (which can be called different things across Windows and Mac devices - we’ll come back to that).

Set your graphics device to AGG

In your Global options, navigate to your graphics options, and set the graphics device to AGG using the dropdown menu.

Your font should now render nicely within your RStudio plot viewer pane when you create a plot using your specified font. Let’s give it a go.

library(ggplot2)trees |> ggplot(aes(x = Girth,  y = Height, size = Volume, colour = Volume)) + geom_point() + labs(title = "Did the font work? Let's hope so!", subtitle = "If it did, your plot should look the same as this.") + theme_minimal() + theme(text = element_text(family = "Karla"), legend.position = "none")

Here what that should look like…

And here is the same graph with the default ggplot fonts, which would indicate the font you chose didn’t get used…

Check everything works when you ggsave()

At this point, your font should be working nicely in your Plot panel in RStudio. You also want it to work when you export a plot using ggsave(). The steps above should mean that it already does, but let’s check that!

In exporting plot, I recommend using a resolution of 300 or higher (I typically go with 400). Remember to also give your plots a background, otherwise they are transparent (take a look at a plot you’ve exported while you’re using dark mode and you’ll see what I mean). Here, I have gone for a white background ("#FFFFFF").

ggsave(filename = "path/to/file.png", dpi = 400, height = 5, width = 8, bg = "#FFFFFF")

In .Rmd and .qmd files, also set the graphics device within the file!

This was the bit that took me the longest to figure out. I could get plots to look great within the viewer panel, and when I exported them, but when I compiled the document they were in, they just reverted back to default fonts. Gah! Thankfully, there’s an easy fix, once you know - phew!

If you are creating plots directly within an RMarkdown (.Rmd) or a Quarto file (.qmd), you also need to specify the graphics device within your knitr::opts_chunk$set() at the start of the file. I typically do this within my first r code chunk:

knitr::opts_chunk$set(echo = TRUE, ..., # any other options you want to specify dev = "ragg_png", dpi = 400,)

Register font variants

If you’ve decided to use a font variant (e.g.“Karla Medium”), there’s another step we need to take to get it to work. In our case, I actually chose the semibold weight, because the medium weight wasn’t heavy enough. I’m telling my device to name Karla in semibold weight “Karla Medium”. We can call it “Custom Font” or anything we like, but calling it “Karla Medium” makes things clearer when we’re writing code!

systemfonts::register_variant(name = "Karla Medium", family = "Karla", weight = "semibold")

This means I can then use this “Karla Medium” font for visualisations. See how the font is heavier than in the previous example.

trees |> ggplot(aes(x = Girth,  y = Height, size = Volume, colour = Volume)) + geom_point() + labs(title = "Did the font work? Let's hope so!", subtitle = "The letters are thicker this time") + theme_minimal() + theme(text = element_text(family = "Karla Medium"), legend.position = "none")

We need to do this font registering step whenever we want to use the font. I’ve either done that via an .onLoad() function within packages I’ve created for my clients, or popped the font-registering line of code at the start of a script / qmd file where I know I’m going to need it.

If the “semibold” font weight isn’t working, we need to look at what that weight is called in your system. Use systemfonts::system_fonts() |> View() to bring up the fonts dataframe again, and take a look at the weight column for the Karla family to see if we need to make a change to how we point systemfonts to it within the register_variant() call. Once you have aligned what you’re calling with weight with what it’s called on your device, you should be good to go!

If you’ve got this far, and your chosen font still doesn’t work, give me a shout and we’ll add a section to this post once we’ve figured out why together!

Additional resources

In creating this post, I’m indebted to June Choe for his work in demystifying fonts. He has a great blog post with lots of other fun font tricks to explore.

I’m also grateful for the patience and perseverance of David Keyes and the band of merry consultants at R for the Rest of Us for all the troubleshooting we have done over the years to get fonts to work across different devices for different client projects. David’s upcoming book “R Without Statistics” is sure to be a great resource!

If you are looking for font inspiration for your next project, I highly recommend Oliver Schöndorfer’s resources. Designing for dataviz is similar to designing for UI: readability is key, and we need to make the most of text hiearchy.

Finally, if you want to see for yourself the difference that paying attention to typography makes in visualisations and tables, I gave a talk about it at the 2023 Shiny in Production conference, which you can rewatch here for some before/after comparisons using data from the Great British Bake Off. The slides are also on this page - feel free to reuse as much of the code as you like!

  • Cara Thompson - Dynamic annotations: Ten tips for better text

Reuse

Citation

For attribution, please cite this work as:

“Getting Fonts to Work in R.” 2024. January 12, 2024. https://www.cararthompson.com/posts/2024-01-12-using-fonts-in-R-for-dataviz/2024-01-12_getting-fonts-to-work.html.

Building Stories With Data - Getting fonts to work in R (2024)

FAQs

What fonts can be used in R? ›

2023-12-08
R familyFont on WindowsFont on Unix
sansArialArial
serifTimes New RomanTimes
monoCourierCourier
symbolStandard Symbols LSymbol
Dec 8, 2023

How do I change the font in R? ›

In this approach to change the font of the given plot, the user needs to call the windowsFont() which is one of the in-build function of the R programming language, with the name of the font as its parameter, this function is used to specify the font family as per requirement and with this, the user also need to use ...

What are the font families available in R? ›

By default there are three font families loaded automatically, i.e., "sans", "serif" and "mono". If one wants to use other fonts, font_add() needs to be called to register new fonts by specifying a family name and corresponding font files.

How do I activate custom fonts? ›

Add a font
  1. Download the font files. ...
  2. If the font files are zipped, unzip them by right-clicking the .zip folder and then clicking Extract. ...
  3. Right-click the fonts you want, and click Install.
  4. If you're prompted to allow the program to make changes to your computer, and if you trust the source of the font, click Yes.

Where does R look for fonts? ›

Default search paths will be assigned when package is loaded: For Windows, it is %windir%\Fonts , usually expanded into C:\Windows\Fonts. For Mac OS, default paths are /Library/Fonts and ~/Library/Fonts and their subdirectories. For Linux and other Unix-like OS, /usr/share/fonts , /usr/local/share/fonts , ~/.

What is the best font for R? ›

R tends to play nicer with . ttf fonts than other types of fonts. There are ways round that, but things will be easier if you choose a static font that has . ttf files!

What font is used in R Ggplot? ›

If you export a figure created using ggplot2 (using RStudio: Export -> Copy to Clipboard) and load it into a graphics editor you can select and edit each individual aspect of the figure, including text. Using Inkscape, the default font for all my ggplot2 plots is Arial.

What is the default font in RStudio? ›

RStudio uses the best font on any system. For example, on Ubuntu, it uses Ubuntu Mono by default. On Windows, it should use Consolas by default, and Lucida Console as a fallback. Consolas is optimized for programming, whereas Courier New is a holdover from the 1950s.

How do I change the default font in ggplot2? ›

How can I change the default font size in ggplot2? Set base_size in the theme you're using, which is theme_gray() by default. The base font size is 11 pts by default. You can change it with the base_size argument in the theme you're using.

How many font families should I use? ›

Mistake 1: Too Many Fonts

A website should use no more than two different font families. More than that will begin to look too busy and your user will leave and find an easier to read website and never to return. Tragic. Woo them with the beautiful simplicity of your thoughtful font choices.

How do I add a font to RStudio editor? ›

1. Install your font as usual for your OS 2. Then restart RStudio 3. The new font appears in the Editor Fonts pull down menu (navigation: Global Options -->Appearance --> Editor Fonts) 4.

How do I import a font into design? ›

You can add fonts from within InDesign (2019 version and above) using Adobe Fonts. To install a font file in Windows, right-click the font file and click "Install." Double-click a font file on Mac to open it in Font Book. Then click "Install."

How do I add custom fonts to Slider Revolution? ›

Add Custom Font to Slider Revolution Global Settings

To include multiple font families, click the “Add Custom Font” button to create a new entry for each additional font family. If you define the @font-face blocks for all the font families in one “custom-font. css” file, you can use the same /webfont/custom-font.

How do I import a custom font family? ›

The @font-face CSS rule explained below is the most common approach for adding custom fonts to a website.
  1. Step 1: Download the font. ...
  2. Step 2: Create a WebFont Kit for cross-browsing. ...
  3. Step 3: Upload the font files to your website. ...
  4. Step 4: Update and upload your CSS file. ...
  5. Step 5: Use the custom font in your CSS declarations.
Jan 9, 2023

Top Articles
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 5452

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.