Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom OTF fonts in ggplot2

I need to use a custom font, namely "Archer", with ggplot2 in R. Archer is an otf typeface installed on my system (Mac OSX Yosemite).

This script (found here: Modifying fonts in ggplot2) doesn't work for Archer, but works fine with other fonts such as Arial.

install.packages("extrafont");library(extrafont)
font_import("Archer")
library(ggplot2)
qplot(1:10)+theme(text=element_text(family="Archer"))

Is there something wrong with otf typefaces in particular?

like image 693
Lucien S. Avatar asked May 05 '15 16:05

Lucien S.


People also ask

Can I use OTF instead of TTF?

There is really no issue for the end user as almost all modern applications that uses fonts are able to work with TTF and OTF files. Users don't need to choose one over the other as they can be used together in creating documents or printing layouts.

Can I use OTF as web font?

You can use EOT (Embedded OpenType) files for Internet Explorer and either OTF (OpenType) or TTF (TrueType) for the rest. (Additional options include WOFF (Web Open Font Format) and SVG (Scalable Vector Graphics) but we will stick to more common types here.)


2 Answers

You can try the showtext package, which directly works on OTF fonts.

library(showtext)
font.add("Archer", "Archer.otf")
showtext.auto()
library(ggplot2)
qplot(1:10)+theme(text=element_text(family="Archer"))

Please replace "Archer.otf" by the real filename of your Archer font in the system.

Using showtext does not require embedding the fonts.

like image 98
yixuan Avatar answered Oct 14 '22 21:10

yixuan


You'll need to convert Archer from an OTF to a TTF. From extrafont's github readme:

Presently it allows the use of TrueType fonts with R

I'm amused---I had to do this because my organization also uses Archer. The first hit for this search is purple, so probably that's what I used and it worked just fine.

When having trouble with extrafont it can also be useful to check the available options with fonts(). Then you can verify that your import was successful.

If you save your plot to a PDF, to make sure to embed the fonts as well, using grDevices::embedFonts or extrafont::embed_fonts.

like image 43
Gregor Thomas Avatar answered Oct 14 '22 21:10

Gregor Thomas