Using google fonts in R on windows 10

I want to (and used to) use custom fonts with ggplot on Windows, Mac and Linus (shinyapps.io, see e.g. here). This is a while ago and - somehow - doesn't work anymore..


What I tried in detail:

Step 1: Initial font availability..

windowsFonts()
$serif
[1] "TT Times New Roman"

$sans
[1] "TT Arial"

$mono
[1] "TT Courier New" 

Step 2: Add a Google Font

sysfonts::font_add_google("Permanent Marker")

Step 3: Make sure the font is registered.

sysfonts::font_families()
[1] "sans"             "serif"            "mono"             "Permanent Marker"

windowsFonts()
$serif
[1] "TT Times New Roman"

$sans
[1] "TT Arial"

$mono
[1] "TT Courier New" 

Why does sysfonts see and show the custom font whereas windowsFonts doesn't?

Step 4: Try to use the custom font in a plot:

Using serif works:

ggplot(mtcars) + 
  geom_point(aes(wt, mpg)) + 
  theme(text = element_text(family = "serif"))

enter image description here

Using Permanent Marker doesn't:

ggplot(mtcars) + 
  geom_point(aes(wt, mpg)) + 
  theme(text = element_text(family = "Permanent Marker"))

enter image description here

What am I missing?


Solution 1:

One way is through the showtext package. The procedure is described in detail here and there.

Basically, we need to use showtext_auto() so that the sysfonts fonts are available to ggplot.


A minimal example:

# initial configuration
windowsFonts()
$serif
[1] "TT Times New Roman"

$sans
[1] "TT Arial"

$mono
[1] "TT Courier New"

# load new font
sysfonts::font_add_google("Permanent Marker")
sysfonts::font_families()
[1] "sans"             "serif"            "mono"            
[4] "Permanent Marker"

# use it
showtext_auto()
ggplot(mtcars) + 
    geom_point(aes(wt, mpg)) + 
    theme(text = element_text(family = "Permanent Marker"))

enter image description here