无法使用ggsave()在{PDF中嵌入ggplot2(/ R)字体

时间:2019-08-14 19:42:46

标签: r ggplot2 fonts

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

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()绘制:

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

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

  

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

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

Plot with correct font Plot witn NO text

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

2 个答案:

答案 0 :(得分:1)

您可以考虑使用extrafont软件包:

ISettings[K]

enter image description here

答案 1 :(得分:0)

实际上,ggsave()对我来说似乎很好。错误实际上是将theme(text = element_text(family = "latex"))添加到绘图中。

稍微调整一下示例,

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 :-(")  

ggsave("df_plot.pdf", 
       plot = plot, 
       device = "pdf", 
       dpi = 320)
#Saving 10.7 x 8.01 in image

但是

plot +  theme(text         = element_text(size=10, family="LM Roman 10"))

产生您发现的错误:

  

grid.Call(C_textBounds,as.graphicsAnnot(x $ label),x $ x,x $ y,中的错误:     找不到多边形边缘。

此问题已在此处得到解答: Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : Polygon edge not found

这些建议对您有用吗?