Justin
Justin

Reputation: 351

Installing .otf Font in R

I'd like to use the fonts my organisation uses in ggplot. I have these in an .otf format on my computer. However, after several hours I have still made zero progress in actually using the fonts in plots.

I've tried using showtext/extrafont/sysfont and no luck.

When I call font_files() I do see the fonts (as they are installed on my PC), but when I try to set the familiy argument to the font I get the following errors:

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
4: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database
5: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database
6: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database

Like I said the fonts are installed. I can use them in Word and other programs and can see them in the windows/fonts folder.

With showtext I've ran:

library(showtext)
showtext_auto() 
font_add("Test2", regular = "Font_Regular_2.otf", italic = "Font_Italic_2.otf")
font_families()

And I do see the "Test2" added to the font families, however when I try add it to my plot nothing changes:

theme(plot.title = element_text(color = "blue", size = 28, face = "bold", hjust = 0.5, family = "Test2"))

Afterwards, I've tried everything discussed in this topic but no results.

It'd be great if I can get a step by step explanation, since I'm really not sure where I'm going wrong here.

Initially I want to use export my plots as png, later I plan to export them in PDF for use in publications. I've read that the latter also requires an extra step?

Upvotes: 1

Views: 791

Answers (2)

zhiming chen
zhiming chen

Reputation: 21

I had this problem recently; I installed OTF fonts and found it did not work by using the default path, usually "C:\\Windows\\fonts" (for Windows).

I right clicked the OTF file, visited the properties and found the file location "C:\Users\Admin\AppData\Local\Microsoft\Windows\Fonts - it was not the same as the default path. By passing the file location to font_add() I was able to make it work:

This worked:

sysfonts::font_add(family = "Font Awesome 6",
                   regular = "C:\\Users\\Admin\\AppData\\Local\\Microsoft\\Windows\\Fonts\\Font Awesome 6 Free-Solid-900.otf")

This did not work:

sysfonts::font_add(family = "Font Awesome 6",
                   regular = "Font Awesome 6 Free-Solid-900.otf")

Upvotes: 2

Justin
Justin

Reputation: 351

So a year later, I found a solution. The problem seems to have been related to the location of the font file for some reason. Loading it from the Windows/Fonts folder didn't work.

When I put it somewhere else in the My Documents-folder and gave showtext the path to the files there, it worked.

library(showtext)

showtext_auto() 

font_add("Test2", regular = "C:\\Users\\MYNAME\\Documents\\FOLDERNAME\\Testfont_2.otf")

font_families()

Upvotes: 3

Related Questions