Custom font appear correct in ggplot Rstudio but not when knit with R Markdown

Viewed 56

I am using the font Lato (https://fonts.google.com/specimen/Lato) as part of a package and loading it on windows machines using windowsFonts(Lato = windowsFont("Lato")). This font works well when producing jpg, png, pdf graphics on both Windows and Mac. I am not using extrafont or showtext due to inconsistencies with graphics on Windows and Macs and other error we encountered.

Here is a comparison of default font and Lato. The difference can easily be seen in the dot above the "i", which is a circle in Lato but a square in the default font. This was produced in Rstudio by simply running the chunk and pasting the graphic.

enter image description here

However, when knitting on Windows (not tested yet on Mac), the Lato font does not work. Here is the example and the common warning message

Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : font family 'Lato' not found, will use 'sans' instead

enter image description here

Here is a fully reproducible example, assuming Lato is installed.

---
title: "Untitled"
output: html_document
date: '2022-07-18'
editor_options: 
chunk_output_type: console
---
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(patchwork)
library(extrafont)
# check if you have the fonts installed
get_font_path <- extrafont:::ttf_find_default_path()
  fonts_list <- list.files(get_font_path)
  if(
  all(c("Lato-Regular.ttf" %in% fonts_list,
    "Lato-Bold.ttf" %in% fonts_list,
    "Lato-Italic.ttf" %in% fonts_list)) == FALSE){
    message("Please install Lato here: https://fonts.google.com/specimen/Lato")
  }
  
# for windows machines, load font
windowsFonts(`Lato` = windowsFont("Lato"))
windowsFonts() #Lato will apppear
default_example <- mtcars %>%
  slice(1:10) %>%
  rownames_to_column("mfg") %>%
  ggplot(aes(x=mpg, y=reorder(mfg, mpg), fill=as.factor(cyl)))+
  geom_col()+
  ggtitle("No theme_amplify()")+
  ylab("Manufacturer")+
  theme(legend.position = "none",
        plot.title=element_text(size=14))

font_example <- mtcars %>%
  slice(1:10) %>%
  rownames_to_column("mfg") %>%
  ggplot(aes(x=mpg, y=reorder(mfg, mpg), fill=as.factor(cyl)))+
  geom_col()+
  ggtitle("theme_amplify()")+
  ylab("Manufacturer")+
  theme_minimal(base_family="Lato")+
    theme(legend.position = "none",
        plot.title=element_text(size=14))

default_example + font_example
# in the default font, the dot over the i is square
# with Lato, the dot over the i is a circle

Note that the above code works with a simple base R plot:

plot(1:10)
text(5, 2, "Hi", family = "Lato", cex=3)
text(7, 2, "Hi", font = 2, cex=3)

enter image description here

Update 1: I have confirmed that this is not an issue on Mac, only Windows.

Update 2: When calling dev.list() in R Studio, I get RStudioGD; however, in R Markdown, only png shows up. Could be related?

1 Answers

Using your code, the font, Lato, rendered correctly. However, I'm on Mac. (I didn't use the code that was strictly for Windows, obviously.) Since I'm not able to reproduce your error with the extrafonts package, I thought I would provide an alternative method to control the font.

You can use the showtext package to do this. (You can download new fonts from Google with this package, as well.)

Whether it's on your computer or not, you can call any Google font family like this.

font_add_google("Lato", "Lato") # family & ref name 

This is font_all_google(<font family name>, <name you ref to use this font>). You can make the reference name what you like (it doesn't have to be the font name). If, for any reason, the call for the font does not work, you will see a message from the command font_add_google. Think of it as 'silence is consent.'

You have to tell it to start listening, too.

showtext_auto() # listen!

I have it set up like this.

---
title: "Untitled"
output: html_document
date: '2022-07-18'
editor_options: 
chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(patchwork)
library(showtext)
font_add_google("Lato", "lato")
showtext_auto()
```

Since Lato is extremely similar to the base font, I made the titles a bit bigger and italicized. In Lato, the print a has a hat, like an 'a' in the font in this text in Stack Overflow. However, when you italicize Lato, an 'a' is like the o with a tail. Check it out. Oh, and in the base R plot, I wrote it out a bit more (to add an 'a'). I italicized the Lato text, too.

If my terrible description of hats and tails doesn't make sense, then check the differences in the 'a' in this image I took from the Lato Google font page.

enter image description here

```{r more,echo=F}
default_example <- mtcars %>%
  slice(1:10) %>%
  rownames_to_column("mfg") %>%
  ggplot(aes(x=mpg, y=reorder(mfg, mpg), fill=as.factor(cyl)))+
  geom_col()+
  ggtitle("No theme_amplify()")+
  ylab("Manufacturer")+
  theme(legend.position = "none",
        plot.title=element_text(size = 20, face = "italic"))

default_example
```

```{r another, echo=F}
mtcars %>%
  slice(1:10) %>%
  rownames_to_column("mfg") %>%
  ggplot(aes(x=mpg, y=reorder(mfg, mpg), fill=as.factor(cyl)))+
  geom_col()+
  ggtitle("theme_amplify()")+
  ylab("Manufacturer")+
  theme_minimal(base_family="Lato")+
    theme(legend.position = "none",
          plot.title=element_text(size = 20, face = "italic"))
```

```{r uhhuh, echo=F}
plot(1:10)
text(5, 5, "Words with an a", font = 3, cex=3, family = "Lato")
text(7, 2, "Words with an a", font = 1, cex=3)
```

enter image description here

enter image description here

Related