无法使用ggsave()在PDF中嵌入ggplot2(/R)字体 [英] Cannot embed ggplot2 (/R) fonts in PDF with ggsave()

查看:700
本文介绍了无法使用ggsave()在PDF中嵌入ggplot2(/R)字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ggplot2-plot中添加了一种字体,当在RStudio的绘图查看器中查看时,它可以完美地工作.但是,当我尝试将图另存为PDF时,根本不会打印任何文本(请参见下面的代码和图片):

I have added a font to my ggplot2-plot, and it works perfectly when viewed in RStudio's plot viewer. However, when I try to save the plot as a PDF, NO text at all is printed (see code and pictures below):

df <- data.frame(x = c(1:10), y = c(1:10)) # Dummy data 

plot <- ggplot(df, aes(x, y)) + # Dummy plot
   geom_point() +
   labs(title = "Correct font in R, NO fonts at all in pdf :-(") +
   theme(text = element_text(family = "latex"))

然后我尝试使用以下代码ggsave()绘制:

Then I try to ggsave() the plot with the following code:

 ggsave("df_plot.pdf", 
   plot = plot, 
   device = "pdf", 
   dpi = 320)

但是我收到一条错误消息:

But I get an error message:

grid.Call.graphics(C_text,as.graphicsAnnot(x $ label),x $ x, x $ y,:无效的字体类型

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : invalid font type

下面是带有正确字体的绘图(在RStudio中)+写入我的pdf文件(完全没有字体)的绘图:

Below is the plot with the correct fonts (in RStudio) + the plot that is written to my pdf file (with no fonts at all):

以正确的字体绘制 以无文字的方式绘制

我在这里想念什么?我已经使用extrafont软件包尝试了各种方法,但是pdf也不在此处打印字体(如果打印了 ,则它只是默认字体).

What am I missing here? I've tried various stuff with the extrafont package, but the pdfs don't print the fonts there either (if something is printed, its just the default fonts).

推荐答案

,您可以考虑使用

you may consider using the extrafont package:

library(tidyverse)
library(extrafont)
fonts()
df <- data.frame(x = c(1:10), y = c(1:10)) # Dummy data 
windowsFonts(Calibri = windowsFont("Calibri"))
plot <- ggplot(df, aes(x, y)) + # Dummy plot
  geom_point() +
  labs(title = "Correct font in R, NO fonts at all in pdf :-(") +
  theme(text = element_text(size=15, family= "Tw Cen MT Condensed Extra Bold"))



ggsave("df_plot.pdf", 
       plot = plot, 
       device = cairo_pdf, 
       dpi = 320)

这篇关于无法使用ggsave()在PDF中嵌入ggplot2(/R)字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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