Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fonts not available in R after importing

Tags:

r

fonts

ggplot2

I'm having some trouble importing fonts in the R environment. My end goal is to include my company's custom font(.ttf files) R for use in ggplot and RMarkdown. I've tried breaking down the problem and noticed that the same problem occurs importing regular Windows fonts. The importing doens't throw any errors, but the fonts are not available for use in plots. I'm using R version 3.5.1 running on Windows 10 Pro 1803.

I've tried importing the Windows fonts using the extrafont package as well as using the showtext packakge. I have also tried to manually copy all the Windows ttf files from C:\WINDOWS\Fonts to C:\Users...\Documents\R\R-3.5.1\library\extrafontdb\metrics , same issue persists.

Here's some code chunks with an R base dataset that throws the error:

library(ggplot2)
library(extrafont) 
font_import()

# Only three fonts seem to have been imported...
loadfonts(); windowsFonts()
#$`serif`
#[1] "TT Times New Roman"

#$sans
#[1] "TT Arial"

#$mono
#[1] "TT Courier New"


ggplot(data = esoph) +
  aes(x = agegp, weight = ncases) +
  geom_bar() +
  ggtitle("This is a title") +
  theme(plot.title =  element_text(size = 14, family = "Calibri"))

#Warning messages:
#1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
#  font family not found in Windows font database
#2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
#  font family not found in Windows font database
#3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
#  font family not found in Windows font database

UPDATE: I have also tried importing using package showtext instead of extrafont.

library(ggplot2)
library(showtext)

# Check the current search path for fonts
font_paths()    
# [1] "C:\\WINDOWS\\Fonts"

# List available font files in the search path
font_files() 
#                path                                file                            family            face
# 1   C:/WINDOWS/Fonts                         AGENCYB.TTF                         Agency FB            Bold
# 2   C:/WINDOWS/Fonts                         AGENCYR.TTF                         Agency FB         Regular
# 3   C:/WINDOWS/Fonts                           ALGER.TTF                          Algerian         Regular
# 166 C:/WINDOWS/Fonts             GeorgiaPro-SemiBold.ttf              Georgia Pro Semibold

# Add one of these fonts
font_add("Agency FB", "AGENCYB.ttf")

font_families()
# [1] "sans"         "serif"        "mono"         "wqy-microhei" "Calibri"      "CalistoMT"    "Agency FB"  

showtext_auto() 

ggplot(data = esoph) +
  aes(x = agegp, weight = ncases) +
  geom_bar() +
  ggtitle("This is a title") +
  theme(plot.title =  element_text(size = 14, family = "Agency FB"))

#Warning messages:
#1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#4: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#5: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#6: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#7: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#8: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#9: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#10: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#11: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database
#12: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
#  font family not found in Windows font database

UPDATE I found out that using showtext, importing fonts from Google does work. But when I try to work locally (windows or custom fonts, fonts are not imported correctly. This does work:

library(showtext)
library(ggplot2)
font_add_google("Quattrocento Sans", "Quattrocento Sans")

showtext_auto() 
windows() 

a <- ggplot(data = esoph) +
  aes(x = agegp, weight = ncases) +
  geom_bar() +
  ggtitle("This is a title") +
  theme(plot.title =  element_text(size = 14, family = "Quattrocento Sans"))
 
print(a) 

I have limited experience in Windows systems so I don't really know where to start. I apologise if this message is a duplicate - I wasn't able to find any problem like it for Windows. Any help would be greatly appreciated!

like image 943
Hilde Avatar asked Dec 05 '25 15:12

Hilde


2 Answers

You can modify the Graphic Device for RStudio to AGG and work with the fonts in a seamless way. Just like the default ones (Changing just the "family" on theme()). Just install the ragg package and follow:

Tools > Global options > General > Graphics > Backend: AGG

Documentation: https://ragg.r-lib.org/

like image 156
Bruno Mioto Avatar answered Dec 07 '25 06:12

Bruno Mioto


Posted by OP.

The command loadfonts(device = "win") is necesary to properly prepare the fonts for use. Add this after loading the fonts. Seems like font_import is the install_packages() cue, and loadfonts() is the library cue. This should work:

library(ggplot2)
library(extrafont) 
font_import()
loadfonts(device = "win")

ggplot(data = esoph) +
  aes(x = agegp, weight = ncases) +
  geom_bar() +
  ggtitle("This is a title") +
  theme(plot.title =  element_text(size = 14, family = "Calibri"))
like image 27
Artem Avatar answered Dec 07 '25 07:12

Artem