grid.Call(C_textBounds,as.graphicsAnnot(x $ label),x $ x,x $ y,中的警告:字符0x20的字体宽度未知 [英] Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font width unknown for character 0x20

查看:844
本文介绍了grid.Call(C_textBounds,as.graphicsAnnot(x $ label),x $ x,x $ y,中的警告:字符0x20的字体宽度未知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ggplot2图形中使用免费字体 Lato 我的R markdown文档的其余部分都使用这种字体设置.

I would like to use the free font Lato in ggplot2 graphs since the remainder of my R markdown document is set in this font.

该字体已安装在我的系统上,并且在字体簿"中可用(仅一次).

The font is installed on my system and available in the Font Book (once only).

所有可用字体都已随extrafont程序包一起加载,并已在extrafontdb中注册.

All available fonts are loaded with the extrafont package and registered in the extrafontdb.

当我将markdown文档编织为PDF时,所有文本均在Lato中正确排版.但是,我的ggPlots的图标签未显示.

When I knit my markdown document as PDF, all text is correctly typeset in Lato. However, the plot labels of my ggPlots are not shown.

我还收到以下警告消息:

I also receive the following warning message:

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

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

After embedding the fonts contained in the document with extrafont::embed_fonts the plot labels are shown for all figures using Lato as font, but

  • 剧情标签在单词之间不包含空格,
  • 所有引用(内部链接,URL,引用)不再起作用.

下面提供了一个MWE,其中包含带有和不带有Lato的ggPlot图形作为字体( Lato可在此处免费获得) 要随后嵌入字体,需要运行embed_fonts("TestRmd.pdf", outfile="TestRmd_embedded.pdf")

An MWE including ggPlot figures with and without Lato as the font is provided below (Lato is freely available here) To embed the fonts afterwards one needs to run embed_fonts("TestRmd.pdf", outfile="TestRmd_embedded.pdf")

任何帮助将不胜感激!

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

插件:

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

AddOn:

An even simpler problem but likely related to the same issue:

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上使用cairo_pdf(例如 1 2 )网站无济于事,并导致以下错误:

The plot in the saved pdf does not contain any labels. Using cairo_pdf as recommend on several SO (e.g. 1, 2) sites does not help and results in the following error:

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

推荐答案

我尝试使用extrafont使其工作,但未成功.我仍然不太确定,但是我认为这是一个错误.这是使用软件包showtext的解决方案:

I tried to make it work using extrafont but did not succeed. I am still not quite sure but I think it is a bug. Here is a solution using the package 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"))
```

这篇关于grid.Call(C_textBounds,as.graphicsAnnot(x $ label),x $ x,x $ y,中的警告:字符0x20的字体宽度未知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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