I'm using ggplot2 in R to generate figures for a publication in which all figures need to be .eps format and all fonts needs to be Arial. I've been following this guide to doing this using the extrafont package. As I understand it, the line loadfonts(device = "postscript")
should register all of the fonts I imported (which includes Arial) with the postscript device. But when I run my code and try to save my figure using this code:
ggplot() + geom_point(aes(x=xvar, y=yvar)) + theme_minimal(base_family = "Arial")
library(extrafont)
font_import()
loadfonts(device = "postscript")
ggsave(filename = "myfile.eps")
I still get this error:
Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : family 'Arial' not included in postscript() device
What am I missing?
Assuming you are on a Windows OS, you can alternatively create the graph using the showtext package.
The drawback is that you cannot use the
ggsave()
function, but instead call the device function by yourself.For the usage of the
showtext
package, a README is given at https://github.com/yixuan/showtext.For future reference, I also had trouble using extrafont with Arial (but on Windows), and it turned out there were multiple causes.
The errors I received were:
Error in title(...) : metric information not available for this device
,In title(...) : font metrics unknown for character 0x4d
andfont width unknown
.It turns out that extrafont requires ghostscript, which is not at all clear from these error messages. In fact, I had ghostscript installed, but (at least in my case) it also had to be registered in the Path environment variable, as described here (substitute your version number).
Even with that in place, it appears that it is not the font names listed in
names(postscriptFonts())
andfonttable()
that can be used.Arial
andTimes New Roman
(orTimesNewRoman
orTimesNewRomanPSMT
) don't work for me, insteadArialMT
andTimes
do. I don't know why this is and how one can find a list of names that do work.Instead of
extrafont
package