Custom Fonts in R Graphs with showtext


The showtext package in R is designed to make it easy to use custom fonts in R graphics, including plots created with base R, ggplot2, and other graphics systems.
This post showcases the key features of showtext and provides a set of graph examples using the package.

Documentation

{showtext}

Quick start


The showtext package in R allows you to easily use custom fonts in R graphics, including plots created with base R, ggplot2, and other graphics systems.

It provides a simple interface to use system fonts and web fonts in R plots, making it easy to create publication-quality graphs with custom typography.

✍️ author → Yixuan Qiu

📘 documentationCRAN

⭐️ more than 500 stars on github

Installation


To get started with showtext, you can install it directly from CRAN using the install.packages function:

install.packages("showtext")

Basic usage


The showtext package allows you to use custom fonts in your R graphics by loading font files and enabling showtext rendering.

Here’s a basic example:

library(showtext)
font_add_google("Pacifico", "pacifico")
showtext_auto()

plot(1:10, main = "Custom Font Plot", family = "pacifico")

Key features



→ Loading Fonts

showtext can load fonts from various sources, including system fonts, font files, and Google Fonts.

  • font_add_google(): simplifies adding Google Fonts by fetching and registering fonts directly from the Google Fonts repository using just the font family name.
font_add_google("Roboto", "roboto")


→ Automatic Font Rendering

The showtext_auto() function enables automatic font rendering for all graphics devices.

showtext_auto()


→ Supporting Various Graphics Systems

showtext works with base R graphics, ggplot2, and other graphics systems.

ggplot(mtcars, aes(wt, mpg)) +
   geom_point() +
   labs(title = "Custom Font in ggplot2") +
   theme(text = element_text(family = "roboto", size = 16))


→ Unicode Support

showtext provides full Unicode support, allowing you to use non-Latin characters and symbols easily.

Example:

font_add_google("Noto Sans JP", "notosans")
plot(1:10, main = "日本語のタイトル", family = "notosans")




❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥