使用extrafont包在.eps图形中使用Arial字体时出现错误

5

我正在使用R中的ggplot2为一份出版物生成图形,其中所有图形都需要是.eps格式,所有字体都需要是Arial。我一直在按照this guide使用extrafont包来实现这一点。据我所知,代码loadfonts(device = "postscript")应该将我导入的所有字体(包括Arial)注册到postscript设备上。但是当我运行我的代码并尝试使用以下代码保存我的图像时:

ggplot() + geom_point(aes(x=xvar, y=yvar)) + theme_minimal(base_family = "Arial")
library(extrafont)
font_import()
loadfonts(device = "postscript")
ggsave(filename = "myfile.eps")

我仍然遇到这个错误:

在grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : 在postscript()设备中未包含字体“Arial”

我错过了什么?

4个回答

7

当我使用extrafonts包和ggsave()保存eps文件时,我也遇到了这个问题。我知道这个帖子已经很老了,但是我的解决方法或许可以帮助其他人:)

R不擅长字体嵌入。然而,在过去,R已经包含了Cairo图形库(因此您不需要再安装Cairo包!),它能够处理嵌入操作。要使用Cairo图形库保存.eps文件,只需使用cairo_ps作为device:

ggsave(filename="Fig1.eps", plot = last_plot(), device = cairo_ps)

6
假设您使用的是Windows操作系统,您可以使用showtext软件包创建图表。 showtext
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()

缺点是您无法使用ggsave()函数,而是要自己调用设备函数。
关于showtext包的使用,请参考https://github.com/yixuan/showtext上的README文档。

谢谢您的建议,但我正在使用Linux Mint。 - seaotternerd
1
然后,您只需要将“arial.ttf”更改为您的Arial字体文件的位置。可以相应地设置粗体、斜体和粗斜体字体,但不是必需的。 - yixuan
太好了!这给了我一个透明度错误,但是我通过使用cairo_ps解决了它,正如这里建议的那样:https://dev59.com/CG865IYBdhLWcg3wEaYx - seaotternerd
另外,实际上我并不需要指定不同路径到我的Arial字体文件。 - seaotternerd
1
showtext 将尝试搜索一些标准字体目录 (?font.paths())。如果文件在这些目录中,则只需要字体的文件名,而不一定是整个路径。 - yixuan

5

供日后参考,我在Windows上使用extrafont与Arial遇到了问题,结果发现有多种原因导致问题。

我收到的错误信息是:Error in title(...) : metric information not available for this deviceIn title(...) : font metrics unknown for character 0x4dfont width unknown

事实证明,extrafont需要ghostscript,这从这些错误消息中并不清楚。实际上,我已经安装了ghostscript,但(至少在我的情况下)它还必须在Path环境变量中注册,如此处所述(替换您的版本号)。

即使在这种情况下,似乎也不能使用names(postscriptFonts())fonttable()中列出的字体名称。对于我来说,ArialTimes New Roman(或TimesNewRomanTimesNewRomanPSMT)不起作用,而是使用ArialMTTimes。我不知道为什么会这样,以及如何找到可用的名称列表。


0

使用其他字体包替代 extrafont

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)

2
哦,实际上这与我已经拥有的有什么不同?我仍然会收到类似的错误消息:“在 grid.Call.graphics(L_text, as.graphicsAnnot(x$label), ... :字体族 'Arial' 在 PostScript 字体数据库中未找到”。 - seaotternerd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接