Custom fonts in R and ggplot2 (2024)

Fonts are one of the most important aspects of a good visualization. Choosing the right font can make a huge difference in the readability and overall quality of a chart. showtext and ragg are two R packages that help to work with custom fonts in R and ggplot2.

  • showtext provides a set of functions that make it easy to use various types of fonts in R plots
  • ragg provides graphic devices based on the AGG library, which gives direct access to all system fonts, making the usage of custom fonts painless and easy.

The following two fonts are going to be used in along this post:

  • Hydrophilia Iced from Floodfonts. Here you can download a zipped folder that contains the font as both .otf and .ttf types.
  • Special Elite from Google Fonts here.

The easiest way to add a custom font is to use font_add_google(). This function will search the Google Fonts repository for a specified family name, download the proper font files, and then add them to sysfonts (an auxiliar package that makes showtext work). See how simple it is in practice:

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

The second argument, family, is optional. It gives the family name of the font that will be used in R. In other words, it means that the name used to refer to the font in R does not need to be the same than the original name of the font. In this case, the font Special Elite is going to be the special family.

note: if the font wanted is not available on Google Fonts, one can use font_add(). The first argument is like the family above, and the second argument is a path to the font file for the font face (both .ttf and .otf work). Not that you have to download the font locally and update the path below

font_add("hydrophilia", "~/Downloads/Hydrophilia/HydrophiliaIced-Regular.ttf")

And last but not least, showtext_auto() must be called to indicate that showtext is going to be automatically invoked to draw text whenever a plot is created.

showtext_auto()

Now it’s a good time to make a plot to showcase the fonts just imported and see how they look like.

library(ggplot2)data <- data.frame(x = 1:4, y = 1:4)ggplot(data) + geom_point(aes(x, y), size = 10, color = "cadetblue4") + geom_label( aes(x, y),  data = data.frame(x = 3, y = 2),  label = "This is Hydrophilia Iced!", family = "hydrophilia", # Use Hydrophilia Iced for the label, size = 7 ) +  labs( x = "Horizontal Label", y = "Vertical label too!", title = "Do you like titles with nice-looking fonts?" ) + theme( # Special Elite for both axis title and plot title axis.title = element_text(family = "special"), title = element_text(family = "special") )

Custom fonts in R and ggplot2 (1)

Cool! That worked pretty well!

However, it’s good to note some caveats with showtext before jumping to the next section:

  • If you are using showtext in a common R script, set dpi according to the device you use to export your figure via showtext_opts(dpi = dpi). For example if you use ggsave(), then you need to set up a dpi of 300.
  • If you are using showtext in RMarkdown documents you don’t have to use showtext_auto(). That will set up the wrong dpi and the text will look too small. You need to add fig.showtext=TRUE to the chunk settings as shown here.
  • For more information about these issues see here.

This solution provided in this section is quite different from the solution above. Instead of using a library to install and manage fonts that are accesible by R, this solution is based on using a different graphic device provided by ragg.

Among other very nice features, using ragg gives access to all system fonts, which means that custom fonts can be used without having to install other package in R.

Assuming RStudio is used to work with R, ragg can be set up as the graphic back-end to the Rstudio device (for RStudio >= 1.4) by choosing AGG as the backend in the graphics pane in general options (see screenshot)

Custom fonts in R and ggplot2 (2)

Also, ragg can be used with RMarkdown documents. knitr supports png output from ragg by setting dev = "ragg_png" in the chunk settings or globally with knitr::opts_chunk$set(dev = "ragg_png").

Finally, if you are going to export your plot with ggsave(), you can simply pass device functions from ragg into the device argument as ggsave("image.png", device=ragg::agg_png).

In practice, you need to download and install the fonts in your system manually. This is usually done by opening the font file and clicking on the install button in the window that pops up. One of its advantages is that this procedure is required only once per font. After a font is installed in your system it can be used anywhere in your R plots without having to use any external packages such as showtext.

Let’s generate the same plot than above, but using the ragg::agg_png backend.

library(ragg)
# Quick notes:# * No "showtext_auto()" or similar calls# * Full names must be used for the fonts because they are now # searched in the systemdata <- data.frame(x = 1:4, y = 1:4)ggplot(data) + geom_point(aes(x, y), size = 10, color = "cadetblue4") + geom_label( aes(x, y),  data = data.frame(x = 3, y = 2),  label = "This is Hydrophilia Iced!", family = "Hydrophilia Iced", size = 7 ) +  labs( x = "Horizontal Label", y = "Vertical label too!", title = "Do you like titles with nice-looking fonts?" ) + theme( axis.title = element_text(family = "Special Elite"), title = element_text(family = "Special Elite") )

Custom fonts in R and ggplot2 (3)

Custom fonts in R and ggplot2 (2024)

FAQs

How do I use custom fonts in R? ›

Save Custom Fonts in R package
  1. Install Internal Package.
  2. Run Function called: setup_font() ( Or whatever name we assign)
  3. This function now goes through the files inside the R Package, look for the fonts, then install the library extrafont if necessary.
Dec 18, 2021

Can I change font in Ggplot? ›

showtext and ragg are two R packages that help to work with custom fonts in R and ggplot2 . ragg provides graphic devices based on the AGG library, which gives direct access to all system fonts, making the usage of custom fonts painless and easy.

Which font is used in R? ›

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

How to add custom text to ggplot? ›

Adding text with geom_text()

It works pretty much the same as geom_point() , but add text instead of circles. A few arguments must be provided: label : what text you want to display. nudge_x and nudge_y : shifts the text along X and Y axis.

Can you change fonts 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 ...

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 font ggplot size? ›

Typically you specify font size using points (or pt for short), where 1 pt = 0.35mm. ggplot2 provides this conversion factor in the variable . pt , so if you want to draw 12pt text, set size = 12 / . pt .

How do I change the font of a label in ggplot? ›

Change Font of ggplot Title and Data Label
  1. for example, if you want to change the x axis font, you should add family = "Comic Sans MS" into element_text in axis. text. x . It is same for y axis or legend. ...
  2. When I edit the code to plot. title = element_text(family = "Comic Sans MS", hjust = 0) nothing changes. – Bren85.
Mar 23, 2022

How do I manually change labels in ggplot? ›

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

How to install system fonts in R? ›

Table of contents
  1. Choose a font.
  2. Check {systemfonts} is installed.
  3. Check the font you want to use is installed.
  4. Set your graphics device to AGG.
  5. Check everything works when you ggsave()
  6. In . Rmd and . qmd files, also set the graphics device within the file!
  7. Register font variants.
  8. Additional resources.
Jan 12, 2024

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.

What is the default font size in R? ›

For all of R's graphical devices, the default text size is 12 points but it can be reset by including a pointsize argument to the function that opens the graphical device.

How do I add a font to RStudio editor? ›

Install fonts
  1. Open up RStudio, if you haven't already.
  2. Navigate to Tools > Global Options > Appearance.
  3. Use the Editor Font box to select Fira Code iScript.
Dec 18, 2018

How do I change the font style 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 ...

References

Top Articles
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 6164

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.