13

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!

Artem
  • 3,304
  • 3
  • 18
  • 41
Hilde
  • 131
  • 1
  • 1
  • 5
  • Have you tried installed only `Calibri` for `R` https://stackoverflow.com/a/51888677 ? – Tung May 10 '19 at 07:09
  • 1
    Do you see `Calibri` when you type `loadfonts(); windowsFonts()` ? – Tung May 10 '19 at 07:10
  • Hi Tung, thanks for your reply! I found that question and this doesn't work for me either: `font_import(pattern = "Calibri", prompt = FALSE)`. Running `loadfonts(); windowsFonts()` returns only "TT Times New Roman" "TT Arial and "TT Courier New" so nothing seems to have been imported..? – Hilde May 10 '19 at 07:15
  • If everything fails, give `showtext` a try https://stackoverflow.com/a/51906008/ – Tung May 10 '19 at 07:15
  • that means you weren't successful importing fonts to R. You should post everything from R console (starting from `library(extrafont)`) so that it's easier to help. – Tung May 10 '19 at 07:18
  • Thanks for the feedback, I've tried using extrafont as well, see my updated question. I've tried to be more clear now about what I've posted to the R console – Hilde May 10 '19 at 07:30
  • What if you run `windows()` after `showtext_auto()` and before `ggplot(...)` call? – Tung May 10 '19 at 07:51
  • That creates the figure in a pop-up window instead of displaying the figure in the R viewer, without the correct font, and throws the same error: `Warning messages: 1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font family not found in Windows font database` – Hilde May 10 '19 at 07:53
  • 1
    Hi, managed to fix it! I think the loading didn't work properly, using `loadfonts(device = "win")` I got access to all the windows fonts and they showed up using `loadfonts(); windowsFonts()`. Thanks for your help – Hilde May 10 '19 at 10:11
  • Glad you fixed it. FYI, you can use `loadfonts(device = 'all')` on all platforms if you install development version on Github https://github.com/wch/extrafont/blob/master/R/load.r#L33 – Tung May 10 '19 at 16:31
  • Weird... Your first code sample that uses showtext completely works for me. – yixuan May 14 '19 at 18:52
  • Hilde, would you like to add your solution as an answer to your own post? I know you edited your question to include it, but it would be nice to have as an answer. – teunbrand Dec 14 '21 at 09:50

2 Answers2

3

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/

Bruno Mioto
  • 382
  • 1
  • 10
  • 1
    This is it !!! I've been searching for a solution for some time now, as `loadfonts(device = "win")` didn't work in my case ! Thank you so much – DanielMc Dec 03 '22 at 08:50
1

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"))
Artem
  • 3,304
  • 3
  • 18
  • 41