使用R中嵌入的字体将ggplot图形保存为PDF [英] Saving ggplot graph to PDF with fonts embedded in r

查看:174
本文介绍了使用R中嵌入的字体将ggplot图形保存为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在遵循我在网上找到的关于将ggplot图形保存为PDF的建议,但是我无法完全使它正常工作.我正在使用extrafont包在Calibri中生成带有文本的图表,但是我的图表却没有文本打印.我不知道我在想什么.我在过程中找不到任何错误.希望其他人可以提供帮助.

I've been following advice I've found online for saving a ggplot graph to PDF but I can't quite get it to work. I'm using the extrafont package to produce charts with text in Calibri, but my charts are printing out with no text. I don't know what I'm missing. I can't find any mistakes in my process. Hopefully, someone else can help.

这是我使用的代码和过程:

Here's the code and process I used:

library(extrafont)
font_import(pattern="[C/c]alibri")
loadfonts(device="win")

我此时安装了GhostScript.然后运行以下命令来设置GhostScript位置.

I installed GhostScript at this time. Then ran the following to set the GhostScript location.

Sys.setenv(R_GSCMD = "C:\\Program Files\\gs\\gs9.21\\bin\\gswin64c.exe")

然后我使用ggplot生成了一个称为图表"的图表.该图表在RStudio中看起来很完美,但在PDF中却不完美.

I then produced a chart using ggplot called "chart". The chart looked perfect in RStudio, but not in PDF.

ggsave("chart.pdf", plot = chart, width = 6, height = 4)

在这里,我收到警告,显示如下内容:

Here I get warnings showing stuff like this:

在grid.Call(C_textBounds,as.graphicsAnnot(x $ label),...:PostScript字体数据库中找不到字体家族'Calibri'

In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... : font family 'Calibri' not found in PostScript font database

显然,这些警告应该发生吗?然后我跑...

Apparently, these warnings are supposed to happen? Then I run...

embed_fonts("chart.pdf", outfile="chart_embed.pdf")

不幸的是,毕竟,最终的嵌入"图表看起来与生成的原始图表没有什么不同,两者都没有任何文字.

Unfortunately, after all this, the final "embed" chart looks no different than the original chart produced, neither of which have any text.

如果有帮助,下面是生成图表的代码:

In case it helps, here's the code to produce the chart:

a <- ggplot(data=stats, aes(x=Date))
Chart <- a + geom_point(aes(y=NevadaTotalNonfarmAllEmployees)) + 
      xlab("Date") + 
      ylab("Nonfarm Jobs") + 
      ggtitle("Nevada Total Jobs") + 
      theme(axis.title.x = element_text(size=15, family = "Calibri"),
            axis.title.y = element_text(size=15, family = "Calibri"),
            axis.text.x = element_text(size=10, family = "Calibri"),
            axis.text.y = element_text(size=10, family = "Calibri"),
            plot.title = element_text(hjust=0.5, size=20, family = "Calibri"))

我一直在想办法解决问题.或者,也许不是代码,而是其他东西?无论哪种方式,谢谢您的帮助.

I've been pulling my hair out trying to figure this out. Or maybe it's not the code but something else? Either way, thanks for any assistance.

推荐答案

这里有两个问题:(1)将字体加载到R中;(2)使用可与自定义嵌入字体正确配合使用的PDF编写库

There are a couple issues at play here: (1) loading fonts into R and (2) using a PDF-writing library that works correctly with custom embedded fonts.

首先,正如其他人提到的那样,在Windows上,通常需要运行extrafont::font_import()来向R注册许多系统字体,但是这可能要花一些时间,并且可能会错过TTF和其他类型的字体的字体.解决此问题的一种方法是使用windowsFonts(name_of_font_inside_r = windowsFont("Name of actual font"))即时将字体加载到R中,而无需加载完整的数据库,如下所示:

First, as others have mentioned, on Windows you generally need to run extrafont::font_import() to register many of your system fonts with R, but it can take a while and can miss TTF and other types of fonts. One way around this is to load fonts into R on the fly, without loading the full database, using windowsFonts(name_of_font_inside_r = windowsFont("Name of actual font")), like so:

windowsFonts(Calibri = windowsFont("Calibri"))

这使得在R中只能访问一种字体.您可以使用windowsFonts()进行检查.每次运行脚本时,您都必须运行此行-字体加载不会在会话之间持续.加载字体后,您可以正常使用它:

This makes just that one font accessible in R. You can check with windowsFonts(). You have to run this line each time the script is run—the font loading doesn't persist across sessions. Once the font has been loaded, you can use it normally:

library(tidyverse)
df <- data_frame(x = 1:10, y = 2:11)

p <- ggplot(df, aes(x = x, y = y)) +
  geom_point() +
  labs(title = "Yay Calibri") +
  theme_light(base_family = "Calibri")
p

第二,R在Windows和macOS上都内置的PDF书写设备不能很好地处理字体嵌入.但是,R现在包括Cairo图形库,该库可以很好地嵌入字体.您可以在ggsave()中指定Cairo设备来使用它,这比处理GhostScript要容易得多:

Second, R's built-in PDF-writing device on both Windows and macOS doesn't handle font embedding very well. However, R now includes the Cairo graphics library, which can embed fonts just fine. You can specify the Cairo device in ggsave() to use it, which is easier than dealing with GhostScript:

ggsave(p, filename = "whatever.pdf", device = cairo_pdf, 
       width = 4, height = 3, units = "in")

这篇关于使用R中嵌入的字体将ggplot图形保存为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆