嵌入新字体以在 ggplot2、R 中绘图 (mac)

Embedding new fonts to plot in ggplot2, R (mac)

我一直在尝试将 ggplot2 图导出为 PDF。问题是我一直在我的绘图中添加异国情调的字体,因此导出的 PDF 不显示任何文本。

我确保这样导入我的字体:

library(extrafont)
font_import(pattern = 'Arch')
loadfonts()

我在将 ggplot2 图放入变量“p”后导出为 PDF:

ggsave("myPlot.pdf", plot=p,  width=4, height=6)
embed_fonts("myPlot.pdf", outfile="myPlot_embed.pdf")

然后我收到一条错误消息:

GhostScript was not found

但是,此页面似乎建议在 Mac OS 上不需要其他步骤(Windows 上有一个额外的步骤):https://github.com/wch/extrafont

知道我做错了什么吗?

您引用的 extrafont 软件包的说明指南 (https://github.com/wch/extrafont/blob/master/README.md) 明确指出:

"You must have Ghostscript installed on your system for embedding fonts into PDF files."

首先,请检查您是否安装了 Ghostscript:

  1. 如果是,请确保它在 $PATH 中,或者其安装位置包含在 $PATH 变量中。

  2. 如果没有,先安装Ghostscript。您可以使用下面介绍的方法。


1。安装 MacPorts

首先,安装MacPorts框架。 MacPorts 提供了一个包管理系统和现成的包,允许您安装 GNU 和其他自由软件包的日志。

安装说明会有所不同,具体取决于您的 OS X:

版本

安装 MacPorts 后,运行 Terminal.app window 中的此命令:

sudo port selfupdate

2。安装 Ghostscript

MacPorts 有一个 Ghostscript 包。您可以通过 Terminal.app window:

中的命令像这样安装它
sudo port install ghostscript

此命令将引入并安装 Ghostscript 所需的更多软件包,如 "dependencies"

请注意:

  1. 安装完成后,您将获得一个 Ghostscript 可执行文件 /opt/local/bin/gs。 (/opt/local/bin/会有更多的辅助程序。)

  2. 这要求您将该目录放入 $PATH。因此,将此行放入您的 ~/.bashrc:

    export PATH=/opt/local/bin:$PATH
    

您可以尝试安装 Ghostscript 的其他选项。一个是 HomeBrew -- 但我没有这方面的个人经验。