14.6 Using Fonts in PDF Files

14.6.1 Problem

You want to use fonts other than the basic ones provided by R in a PDF file.

14.6.2 Solution

The extrafont package can be used to create PDF files with different fonts.

There are a number of steps involved, beginning with some one-time setup. Download and install Ghostscript http://www.ghostscript.com/download, then run the following in R:

install.packages("extrafont")
library(extrafont)

# Find and save information about fonts installed on your system
font_import()

# List the fonts
fonts()

After the one-time setup is done, there are tasks you need to do in each R session:

library(extrafont)
# Register the fonts with R
loadfonts()

# On Windows, you may need to tell it where Ghostscript is installed
# (adjust the path to match your installation of Ghostscript)
Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.05/bin/gswin32c.exe")

Finally, you can create a PDF file and embed fonts into it, as in Figure 14.4:

library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
  ggtitle("Title text goes here") +
  theme(text = element_text(size = 16, family = "Impact"))

ggsave("myplot.pdf", width = 4, height = 4)

embed_fonts("myplot.pdf")
PDF output with embedded font Impact

Figure 14.4: PDF output with embedded font Impact

14.6.3 Discussion

Fonts can be difficult to work with in R. Some output devices, such as the on-screen quartz device on Mac OS X, can display any font installed on the computer. Other output devices, such as the default png device on Windows, aren’t able to display system fonts.

On top of this, PDF files have their own quirks when it comes to fonts. The PDF specification has 14 “core” fonts. These are fonts that every PDF renderer has, and they include standards such as Times, Helvetica, and Courier. If you create a PDF with these fonts, any PDF renderer should display it properly.

If you want to use a font that is not one of these core fonts, though, there’s no guarantee that the PDF renderer on a given device will have that font, so you can’t be sure that the font will display properly on another computer or printer. To solve this problem, non-core fonts can be embedded into the PDF; in other words, the PDF file can itself contain a copy of the font you want to use.

If you are putting multiple PDF figures in a PDF document, you may want to embed the fonts in the finished document instead of in each figure. This will make the final document smaller, since it will only have the font embedded once, instead of once for each figure.

Embedding fonts with R can be a tricky process, but the extrafont package handles many of the ugly details for you.

Note

As of this writing, extrafont will only import TrueType (.ttf) fonts, but it may support other common formats, such as OpenType (.otf), in the future.

14.6.4 See Also

Showtext is another package for using different fonts in R graphics. It supports TrueType, OpenType, PostScript Type 1 fonts, and makes it easy to download web fonts. However, it currently does not work correctly with the RStudio viewer pane. See https://cran.r-project.org/web/packages/showtext/vignettes/introduction.html.

For more on controlling text appearance, see Recipe 9.2.