embedFonts 正在更改我的 R 图的边界框

embedFonts is changing the bounding box of my R plot

我在 R 中制作了一个漂亮的图,用于科学期刊。根据期刊的规范,我需要一个嵌入字体的 eps 文件格式。由于 R 不导出带有嵌入字体的 eps 文件,我使用基本图形调用 embedFonts() 来转换它。但是,这个调用正在改变我的图形的边界框。在下面这个简单的例子中,白色 space 被裁剪掉了。在我的 OCD 调整出版质量图中,添加了白色 space,因为我已经将它完美地调整到边缘。

我希望嵌入字体,但其他一切保持不变!

这是一个例子:

setEPS()
postscript(file = "~/Desktop/test.eps", family = "Helvetica", colormodel = "srgb", width = 5, height = 3)
plot(x = 1:10, y = 1:10, col = "red", main = "Keep everything the same but embed my fonts!")
dev.off()
embedFonts(file = "/Users/athena/Desktop/test.eps", format = "eps2write", outfile = "/Users/athena/Desktop/stupid.eps")

到目前为止我有:
- 使用自制软件安装 ghostscript:$ brew install ghostscript
- 了解到 embedFonts 需要完整路径,不允许 tilda
- 将格式指定为 "eps2write" 因为默认 "ps2write" 将其更改为后记

我在 "reproducible research" 上花费了很多精力,包括开放数据、开放代码、开放期刊、bla bla bla...我真的不想使用 illustrator 转换或其他方式制作我的最终数据:(

发生这种情况的原因是因为 embedFonts 在内部调用 Ghostscript,后者又试图通过修剪周围的一些白色 space.[=12= 来拟合 "optimal" 边界框来表现得更聪明]

我们可以通过在 R 中的 5 英寸 x 3 英寸绘图区域的周边绘制一个不可见的框来防止这种情况发生。只需在您的代码片段中再添加一行:

setEPS()
postscript(file = "~/Desktop/test.eps", family = "Helvetica", colormodel = "srgb", width = 5, height = 3)
plot(x = 1:10, y = 1:10, col = "red", main = "Keep everything the same but embed my fonts!")
box(which="outer", col="white")
dev.off()
embedFonts(file = "/Users/athena/Desktop/test.eps", format = "eps2write", outfile = "/Users/athena/Desktop/stupid.eps")

解决此问题的另一种方法是 Jonathan 在这里的回答,它基本上使用 sed 从输入文件中读取边界框信息并将其写入输出文件:http://r.789695.n4.nabble.com/eps-file-with-embedded-font-td903387.html 正如@neilfws 在上面的评论。