Although R has vast graphical functionality I've lamented the lack of support for additional fonts. You can spend an incredible amount of time fine-tuning a
ggplot2 graphic, fiddling with the length of the tick marks, getting the legend just right but then the Helvetica text detracts from the beauty of what you've created.
My understanding is that support for 'non-standard' fonts is extremely difficult given the large number of computing setups, graphics devices etc. Nevertheless we often need to prepare Postscript and PDF plots for scientific papers and reports and the standard fonts often don’t cut it. Luckily there is support for additional fonts particularly if you’re creating PDFs or postscript files. I'm going to show an example using the package
extrafont. There is also a relatively new package called
showtext that I got to work for me, but I didn't find that it offered any functionality beyond
extrafont (and it crashed my R session twice) so I won't cover that package.
Bauhaus 93 Font For Mac Osx
1. Import fonts (and some quick data setup)
extrafont package you'll need to make sure that you have GhostScript on your system in order to embed the fonts (you will also need to tell R where it's located – see below). In addition, you will need to import the fonts you need. Luckily the package comes with a function that does this for you without much fuss – it takes a couple of minutes, depending on how many fonts you have. Here is an example of the code to import and then review the fonts:
In the site I used the 'Bauhaus 93' font which is a standard font on Mac OSX. When I view the site using Safari (or Firefox) on my MacBookPro, it looks fine. However when I view the site on my IPad 2 or IPhone 4 (using Safari) the Bauhaus 93 font is replaced by what looks like Times New Roman (default font). The logo uses Bauhaus 93 and I am struggling to find good fonts to pair with this. Image borrowed from Identifont Particularly I would prefer to only use Bauhaus for the logo and another 1-2 fonts.
Now we're ready to use the fonts in an actual plot. Based on my review of Winston Chang's GitHub repository for the package all computer systems can use
extrafont to embed fonts in PDF/PS files but extra fonts are only available in bitmap output on Windows machines.
2. Use your new fonts – Bauhaus 93 TrueType font anyone?
Below I'm saving directly to an image and the non-default font shows up both on a screen device and in the saved PNG file. But on a Mac you may be limited to saving to PDF (see next setp)
3. Create a PDF (and don't forget to embed your fonts)
You can use
ggsave function to save the plot to PDF.
If you open the PDF now, though, you will be sorely disappointed! The new font will not appear in the PDF because we have not embedded the font in the PDF yet. In order to do this you have two last steps. Tell R where GhostScript is located and then embed the fonts.
And you're ready to send that PDF directly to your journal of choice.
4. Last comment
I found that most of the fonts worked perfectly both on my image devices as well as PDF but in a few cases something went wrong with the PDF. For example, I played around with the Algerian TrueType font and got output like this:
Bauhaus 93 Font
when it should look like this:
Some digging would be required to track down the issue and, fortunately, Algerian TrueType is not high on my list of fonts to use.