【问题标题】:Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font width unknown for character 0x20grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, 中的警告:字符 0x20 的字体宽度未知
【发布时间】:2020-09-15 21:53:37
【问题描述】:

我想在ggplot2 图表中使用免费字体Lato,因为我的R markdown 文档的其余部分都是用这种字体设置的。

该字体已安装在我的系统上并在字体册中可用(仅一次)。

所有可用字体都使用extrafont 包加载并在extrafontdb 中注册。

当我将 Markdown 文档编成 PDF 时,Lato 中的所有文本都已正确排版。但是,我的 ggPlots 的绘图标签没有显示。

我还收到以下警告消息:

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font width unknown for character 0x20

在使用extrafont::embed_fonts 嵌入文档中包含的字体后,所有图形的绘图标签都使用Lato 作为字体显示,但是

  • 绘图标签在单词之间不包含任何空格,
  • 任何引用(内部链接、URL、引文)都不再起作用。

一个 MWE,包括带有和不带有 Lato 的 ggPlot 图形,如下所示 (Lato is freely available here) 之后要嵌入字体,需要运行embed_fonts("TestRmd.pdf", outfile="TestRmd_embedded.pdf")

非常感谢任何帮助!

MWE:

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

```{r echo=FALSE}
library(ggplot2)
```

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

### Load fonts and set font for ggplots globally
```{r include=FALSE}
# install.packages("extrafont") # see https://github.com/wch/extrafont/
library(extrafont)
# font_import()   # run once
loadfonts()       # loadfonts

# globally set ggplot2 theme and font ("Lato Light")
theme_set(theme_minimal(base_size=12, base_family="Lato Light"))
```

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

### Plot with Impact font {#impact}
```{r echo=FALSE, out.width = '30%'}
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(size=16, family="Impact"))
```

### Run to embed fonts
```{r eval=FALSE, include=TRUE}
embed_fonts("TestRmd.pdf", outfile="TestRmd_embedded.pdf")
```

### Links test

Links test 1 (internal reference): [Headline standard](#standard)

Links test 2 (URL): [RStudio has become a Public Benefit Corporation](https://blog.rstudio.com/2020/01/29/rstudio-pbc)

插件:

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

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

保存的 pdf 中的绘图不包含任何标签。在几个 SO(例如12)网站上使用 cairo_pdf 的建议并没有帮助并导致以下错误:

ggsave(p, filename = "iris.pdf", device = cairo_pdf)
# In dev(filename = filename, width = dim[1], height = dim[2], ...) :
#   failed to load cairo DLL

【问题讨论】:

    标签: r ggplot2 r-markdown markdown extrafont


    【解决方案1】:

    我尝试使用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 的文档大小比嵌入字体时要大得多(我的 15mb 报告与 extrafont 变成了一个巨大的 190mb 文件)。
    • 之后使用 Adob​​e Acrobat Pro 也无法减小文件大小(事实上,当我使用“另存为缩小大小的 PDF”功能时,文件大小增加到 230mb...)
    • 当我从上面编织代码时,生成的文件大小为 141kb(Windows 10、knitr 1.28、MikTex 2.9)。你编织我的代码时尺寸是多少?
    • @mavericks 即使我复制上面的块 6 次,文件大小在不使用 Lato 时为 499kb,在使用时为 525kb。我猜这个问题似乎与您的文档有关。
    • 我能够将文件大小从 190mb 减少到 4mb,如下所示:1) 通过全局定义和设置 ggtheme 来简化绘图,2) 如果可行,请尝试立即添加 geoms,即使用 `geom_text(data = df_geom_text, aes(x = x_pos, y = y_pos, label = label))` 在图中做几个注释。尽管如此,使用showtext 的 20 页报告仍然比没有使用时大得多(640kb 对 4mb)
    猜你喜欢
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多