Changing font in R plots (Arial Narrow)

To my surprise, changing fonts in R plots is not a trivial task. It’s even worse if you are asked to change to font to one of the Windows licensed fonts and you are on Linux. I personally think it’s easier and faster to change the font during the editing but some people don’t know how (or don’t want to know how).

First, I tried to use the extrafont package. Even with fixes for “No FontName. Skipping” , Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y”, and trying to install Windows fonts in Ubuntu, I still didn’t get the results.

The best and only solution was to use the showtext package (as found here). You will need to download the font you want. You’ll need the .tff format (.ttc, or.otf). For me, it was Arial Narrow from here.

> # Install showtext package
> install.packages("showtext")
> # Load the library and install the font
> library("showtext") 
> font_add(family = "arialn", regular = "~/Downloads/arialn.ttf")
> # Before plotting, "activate" showtext
> showtext_auto()
> # Do some fancy plots, add theme(text = element_text(family = "arialn")) to all the features where you want to use this font if you are using ggplot2, and save to PDF
> # Deactivate showtext if you no longer need it
> showtext_auto(FALSE)

And that’s it! You’ll have nice plots with the font you want.

Or, you can choose any of the Google free fonts from here. If I didn’t find Arial Narrow I would go with Archivo Narrow. In the case of Google fonts, you don’t need to download anything.

# Get the font from Google fonts
> font_add_google("Archivo Narrow", "archivon")

See more examples in the showtext readme.

But one important thing to remember – this will not store the text as text in the pdf but as curves. If you then import the pdf into a graphics editor, it won’t be easy to edit the text. You would have to convert the curves to text (somehow) and then edit it as text.

Leave a comment