mav*_*cks 8 markdown r ggplot2 r-markdown extrafont
我想在图表中使用免费字体Latoggplot2,因为我的 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字体的图形都会显示绘图标签,但是
下面提供了一个 MWE,包括带有和不带有 Lato 字体的 ggPlot 图形(Lato 可以在此处免费获得)要随后嵌入字体,需要运行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)
Run Code Online (Sandbox Code Playgroud)
一个更简单的问题,但可能与同一问题相关:
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")
Run Code Online (Sandbox Code Playgroud)
保存的 pdf 中的绘图不包含任何标签。cairo_pdf在多个 SO(例如1、2 )站点上按照建议使用没有帮助,并会导致以下错误:
ggsave(p, filename = "iris.pdf", device = cairo_pdf)
# In dev(filename = filename, width = dim[1], height = dim[2], ...) :
# failed to load cairo DLL
Run Code Online (Sandbox Code Playgroud)
我试图让它工作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"))
```
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7989 次 |
| 最近记录: |