Add font to R that is not in extrafonts library

First you get the font you want and install it on your system. Nothing to do with R. Test if the font works by checking in any regular program like MS Word or something.

Then open R, load extrafont package and import the font that you installed. I think it only works with .ttf fonts for now.

library(extrafont)
font_import(pattern="Roboto")

If this works, then this step will add those fonts to the extrafontdb. You will see something like this...

> font_import(pattern="Roboto",prompt=FALSE)
Scanning ttf files in C:\windows\Fonts ...
Extracting .afm files from .ttf files...
C:\Windows\Fonts\Roboto-Black.ttf => C:/R/R-3.5.1/library/extrafontdb/metrics/Roboto-Black
C:\Windows\Fonts\Roboto-BlackItalic.ttf => C:/R/R-3.5.1/library/extrafontdb/metrics/Roboto-BlackItalic
...
C:\Windows\Fonts\RobotoCondensed-Regular.ttf => C:/R/R-3.5.1/library/extrafontdb/metrics/RobotoCondensed-Regular
Found FontName for 30 fonts.
Scanning afm files in C:/R/R-3.5.1/library/extrafontdb/metrics
Writing font table in C:/R/R-3.5.1/library/extrafontdb/fontmap/fonttable.csv
Writing Fontmap to C:/R/R-3.5.1/library/extrafontdb/fontmap/Fontmap...

This is a one time thing. Once imported, it is available within R from then on. All you have to do is run below.

library(extrafont)
# for windows
windowsFonts(sans="Roboto")
loadfonts(device="win")
loadfonts(device="postscript")

Now the defaults should have changed.

plot(x=1:5,y=1:5)

enter image description here

ggplot has base_family which needs to be changed and family argument for text geoms.

library(ggplot2)
p <- ggplot(data.frame(x=1:5,y=1:5),aes(x,y))+
  geom_point()+
  geom_text(aes(label=y),nudge_x=0.5,family="Roboto")+
  theme_bw(base_family="Roboto")
p

Exporting raster images should work too.

ggsave("plot.png",p)

enter image description here

PDFs are a pain. They have an extra family argument. There is also something about embedding and stuff. See link below.

ggsave("plot.pdf",p,family="Roboto")

All the info you need is here.


The solution using the showtext package:

library(showtext)
## Add the font with the corresponding font faces
font_add("national2",
    regular = "National2CondensedTest-Regular.otf",
    bold = "National2CondensedTest-Bold.otf")
## Automatically use showtext to render plots
showtext_auto()

library(ggplot2)
p = ggplot(NULL, aes(x = 1, y = 1)) + ylim(0.8, 1.2) +
    annotate("text", 1, 1.1, label = "National 2 Condensed Bold",
             family = "national2", fontface = "bold", size = 15) +
    annotate("text", 1, 0.9, label = "National 2 Condensed Regular",
             family = "national2", size = 12) +
    theme(axis.title = element_blank(),
          axis.ticks = element_blank(),
          axis.text  = element_blank())

ggsave("test.pdf", p, width = 8, height = 4)

Below is the generated plot:

Output of the plot using showtext

I used a test version of the font files, and in your case simply change the regular and bold arguments to the actual paths of your files.

Tags:

Fonts

R

Ggplot2