ggplot Custom fonts not working in Quarto

When I render a ggplot with custom fonts it works in the IDE however in Quarto. I keep getting this error:

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

For example, here is my script:

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


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

I'm working from this S/O, but it's taking a while to download all the needed fonts. Try this yourself and let us know?

I have the fonts loaded on my system. When I run the plot in RStudio it works great. (see below). It's when I try to render the report in Quarto or RMarkdown it fails.

I missed one step.

extrafont::loadfonts(quiet = TRUE)

Needed at the beginning of the script.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.