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





