使用带有extrafont包的.eps图中的Arial时出错

sea*_*erd 3 fonts r eps ggplot2

我在R中使用ggplot2为出版物生成数字,其中所有数字都需要.eps格式,所有字体都需要是Arial.我一直在关注这个指南,以这样使用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")
Run Code Online (Sandbox Code Playgroud)

我仍然收到此错误:

grid.Call中的错误(L_textBounds,as.graphicsAnnot(x $ label),x $ x,x $ y,:family'Arial'未包含在postscript()设备中

我错过了什么?

小智 7

extrafonts当我使用带有ggsave()for -files 的包时,我也偶然发现了这个问题eps。我知道这个线程已经过时了,但也许我的解决方法将来可能会帮助其他人。:)

\n

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

\n
ggsave(filename="Fig1.eps", plot = last_plot(), device = cairo_ps)\n
Run Code Online (Sandbox Code Playgroud)\n


yix*_*uan 6

假设您使用的是Windows操作系统,则可以使用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()
Run Code Online (Sandbox Code Playgroud)

缺点是你不能使用该ggsave()功能,而是自己调用设备功能.

有关showtext软件包的使用,请参阅https://github.com/yixuan/showtext上的自述文件.


oul*_*enz 5

为了将来参考,我在 Arial 中使用 extrafont 时也遇到了问题(但在 Windows 上),结果证明有多种原因。

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

事实证明,extrafont 需要ghostscript,从这些错误消息中并不清楚。事实上,我已经Ghostscript的安装,但(在我的情况下,至少)它也有在Path环境变量进行注册,如所描述这里(替换您的版本号)。

即使考虑到这地方,看来,它是不是在列出的字体名称names(postscriptFonts())fonttable()可以使用的。ArialTimes New Roman(或TimesNewRomanTimesNewRomanPSMT)不为我工作,而不是ArialMTTimes做的。我不知道这是为什么以及如何找到有效的名称列表。