ggplot 自定义字体在四开中不起作用

Gre*_*ell 5 r ggplot2 r-markdown quarto

当我使用自定义字体渲染 ggplot 时,它可以在 IDE 中工作,但是在 Quarto 和 RMarkdown 中我不断收到此错误:

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  invalid font type
Calls: .main ... drawDetails -> drawDetails.text -> grid.Call.graphics
Run Code Online (Sandbox Code Playgroud)

这些字体都安装在我的系统上,但 Quarto 或 RMakrdown 无法识别。
例如,这是我的脚本:

---
title: "TEST"
format: pdf
editor: visual
---

This plot works:
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(tidyverse)

mtcars |>
  count (cyl) |>  
  ggplot (aes (x = cyl, y = n)) + 
  geom_col() + 
  labs (title = "Plot 1") + 
  geom_text (aes (label = n), 
             vjust = -1)
```

This plot does not:

 ```{r echo=FALSE, message=FALSE, warning=FALSE}
mtcars |>
  count (cyl) |>  
  ggplot (aes (x = cyl, y = n)) + 
  geom_col() + 
  labs (title = "Plot 2") + 
  geom_text (aes (label = n), 
             vjust = -1,
             family = "Montserrat") + 
  theme (text = element_text(size = 12,  family = "Montserrat"))
```
Run Code Online (Sandbox Code Playgroud)

Gre*_*ell 4

错过了一步。我添加了

extrafont::loadfonts(quiet = TRUE)
Run Code Online (Sandbox Code Playgroud)

在脚本的开头,现在可以运行了。