Error using Arial in .eps figure with extrafont package
Asked Answered
H

4

5

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?

Hyaline answered 4/3, 2015 at 19:41 Comment(0)
H
6

Assuming you are on a Windows OS, you can alternatively create the graph using the showtext package.

library(showtext)
## add the Arial font
font.add("Arial", regular = "arial.ttf",
    bold = "arialbd.ttf", italic = "ariali.ttf", bolditalic = "arialbi.ttf")

setEPS()
postscript("some_graph.eps")
showtext.begin() ## call this function after opening a device

ggplot() + geom_point(aes(x=xvar, y=yvar)) +
    theme_minimal(base_family = "Arial")

dev.off()

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.

Heartsick answered 8/3, 2015 at 21:43 Comment(5)
Thanks for the suggestion, but I'm running Linux Mint.Hyaline
Then you just need to change "arial.ttf" to the location of your Arial font file. Bold, italic and bold-italic font faces can be set accordingly, but are not necessary.Heartsick
Excellent! That gave me a transparency error, but I solved it by using cairo_ps, as suggested here: #4001816Hyaline
Also, I didn't actually have to specify a different path to my Arial font file.Hyaline
showtext will try to search some standard font directories (?font.paths()). If the file is within those directories, only the filename of the font is needed, not necessarily the whole path.Heartsick
R
7

I stumbled over this problem as well when using the extrafonts package with ggsave() for eps-files. I know this thread is old, but maybe my workaround may help others in the future. :)

R isn’t good at font embedding. However, R has included the Cairo graphics library in the past (therefore you don’t need to install the Cairo package anymore!), which is able to handle the embedding. To save an .eps using the Cairo graphics library just use cairo_ps as device:

ggsave(filename="Fig1.eps", plot = last_plot(), device = cairo_ps)
Rachal answered 30/1, 2021 at 10:30 Comment(0)
H
6

Assuming you are on a Windows OS, you can alternatively create the graph using the showtext package.

library(showtext)
## add the Arial font
font.add("Arial", regular = "arial.ttf",
    bold = "arialbd.ttf", italic = "ariali.ttf", bolditalic = "arialbi.ttf")

setEPS()
postscript("some_graph.eps")
showtext.begin() ## call this function after opening a device

ggplot() + geom_point(aes(x=xvar, y=yvar)) +
    theme_minimal(base_family = "Arial")

dev.off()

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.

Heartsick answered 8/3, 2015 at 21:43 Comment(5)
Thanks for the suggestion, but I'm running Linux Mint.Hyaline
Then you just need to change "arial.ttf" to the location of your Arial font file. Bold, italic and bold-italic font faces can be set accordingly, but are not necessary.Heartsick
Excellent! That gave me a transparency error, but I solved it by using cairo_ps, as suggested here: #4001816Hyaline
Also, I didn't actually have to specify a different path to my Arial font file.Hyaline
showtext will try to search some standard font directories (?font.paths()). If the file is within those directories, only the filename of the font is needed, not necessarily the whole path.Heartsick
R
5

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 and font 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()) and fonttable() that can be used. Arial and Times New Roman (or TimesNewRoman or TimesNewRomanPSMT) don't work for me, instead ArialMT and Times do. I don't know why this is and how one can find a list of names that do work.

Regression answered 17/2, 2016 at 18:49 Comment(0)
T
0

Instead of extrafont package

library(ggplot2)
plot <- ggplot(mtcars, aes(wt, qsec)) + 
  geom_point() + 
  theme(text = element_text(family = "Arial"),
        axis.title = element_text(size = 22))

ggsave(filename = "myfile.eps",plot)
Tented answered 5/3, 2015 at 0:3 Comment(1)
Uh, how is this different than what I already have, in practice? I still get a similar error: "In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), ... : font family 'Arial' not found in PostScript font database"Hyaline

© 2022 - 2024 — McMap. All rights reserved.