grid.Call(C_textBounds,as.graphicsAnnot(x$label)、x$x、x$y)中的警告:字符0x20的字体宽度未知

grid.Call(C_textBounds,as.graphicsAnnot(x$label)、x$x、x$y)中的警告:字符0x20的字体宽度未知,r,ggplot2,r-markdown,markdown,extrafont,R,Ggplot2,R Markdown,Markdown,Extrafont,我想在ggplot2图形中使用自由字体,因为我的R标记文档的其余部分是用这种字体设置的 字体已安装在我的系统上,并在字体手册中提供(仅一次) 所有可用字体都将加载extrafont软件包,并在extrafontdb中注册 当我以PDF格式编制标记文档时,所有文本都在Lato中正确排版。但是,我的ggPlots的打印标签不会显示 我还收到以下警告消息: grid.Call中的警告(C_textBounds,如.graphicsAnnot(x$label),x$x,x$y,:字符0x20的字体宽度未

我想在
ggplot2
图形中使用自由字体,因为我的R标记文档的其余部分是用这种字体设置的

字体已安装在我的系统上,并在字体手册中提供(仅一次)

所有可用字体都将加载
extrafont
软件包,并在
extrafontdb
中注册

当我以PDF格式编制标记文档时,所有文本都在
Lato
中正确排版。但是,我的ggPlots的打印标签不会显示

我还收到以下警告消息:

grid.Call中的警告(C_textBounds,如.graphicsAnnot(x$label),x$x,x$y,:字符0x20的字体宽度未知

使用
extrafont::embed_font
嵌入文档中包含的字体后,将使用
Lato
作为字体显示所有图形的打印标签,但

  • 打印标签在单词之间不包含任何空格
  • 任何引用(内部链接、URL、引用)都不再有效
一个MWE,包括ggPlot图形,带有和不带Lato,字体如下() 要在以后嵌入字体,需要运行
embed\u字体(“TestRmd.pdf”,outfile=“TestRmd\u embedded.pdf”)

非常感谢您的帮助

MWE: 插件: 一个更简单的问题,但可能与同一问题有关:

library(extrafont)
extrafont::font_import()
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + theme_minimal(base_size=10, base_family="Lato Light")
ggsave(p, filename = "iris.pdf")

我尝试使用
extrafont
使其正常工作,但没有成功。我仍然不太确定,但我认为这是一个错误。下面是使用包
showtext
的解决方案:

---
title: "Embedding Fonts in PDF"
output: pdf_document
urlcolor: blue
---

```{r include=FALSE}
# notice the chunk option 'fig.showtext' that tells R to use the showtext 
# functionalities for each ne graphics device opened
knitr::opts_chunk$set(dev = 'pdf', cache = FALSE, fig.showtext = TRUE)

library(ggplot2)
library(showtext)

font_add(family = "Lato", regular = "/Users/martin/Library/Fonts/Lato-Light.ttf") 
```


### Plot with newly set standard font (= Lato) {#lato}
```{r echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point() +     
  ggtitle("Fuel Efficiency of 32 Cars") + 
  xlab("Weight (x1000 lb)") + 
  ylab("Miles per Gallon") + 
  theme(text = element_text(family="Lato"))
```

感谢您使用
showtext
软件包提出的解决方案,它确实有效!但是,当我使用这种方法时,编织PDF的文档大小要比嵌入字体大得多(我的带有extrafont的15mb报告变成了一个巨大的190mb文件)。使用Adobe Acrobat Pro之后也无法减小文件大小(事实上,当我使用“另存为减小大小的PDF”功能时,文件大小增加到230mb…)当我从上面编写代码时,生成的文件大小为141kb(Windows 10,knitr 1.28,MikTex 2.9).mavericks即使我复制了6次上述数据块,文件大小仍然是499kb(不使用Lato),而使用Lato时是525kb。我猜问题似乎与您的文档有关。我能够将文件大小从190mb减少到4mb,如下所示:1)通过全局定义和设置
ggtheme
来简化绘图,2)如果可行,尝试立即添加
geom
,即使用`geom_text(data=df_geom_text,aes(x=x_pos,y=y_pos,label=label))`在绘图中进行多个注释。尽管如此,使用showtext的20页报告仍然比不使用showtext的要大得多(640kb对4mb)
ggsave(p, filename = "iris.pdf", device = cairo_pdf)
# In dev(filename = filename, width = dim[1], height = dim[2], ...) :
#   failed to load cairo DLL
---
title: "Embedding Fonts in PDF"
output: pdf_document
urlcolor: blue
---

```{r include=FALSE}
# notice the chunk option 'fig.showtext' that tells R to use the showtext 
# functionalities for each ne graphics device opened
knitr::opts_chunk$set(dev = 'pdf', cache = FALSE, fig.showtext = TRUE)

library(ggplot2)
library(showtext)

font_add(family = "Lato", regular = "/Users/martin/Library/Fonts/Lato-Light.ttf") 
```


### Plot with newly set standard font (= Lato) {#lato}
```{r echo=FALSE, out.width = '100%'}
ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point() +     
  ggtitle("Fuel Efficiency of 32 Cars") + 
  xlab("Weight (x1000 lb)") + 
  ylab("Miles per Gallon") + 
  theme(text = element_text(family="Lato"))
```