Cannot import fonts into R

1.8k views Asked by At

I'm aware this issue has been posted on already, but I feel like I've tried most of the solutions without finding any success.

I'm using hrbrthemes to plot my ggplot graphs, and it keeps telling me I don't have the right fonts imported.

I installed all the fonts on windows, I used the extrafont packages and ran the font_import() command. I keep getting the following errors:

C:\Windows\Fonts\RobotoCondensed-Bold.ttf : No FontName. Skipping.

For absolutely all the fonts. Yet if I go to C:\Windows\Fonts, you can see from this image that the RobotoCondensed font family is properly installed.

enter image description here

The loadfonts(device = "win") also doesn't do anything for me. I've tried all combinations including restarting my r session in between things and I still get this when I check what fonts are available:

enter image description here

Here's my session info:

> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=English_Switzerland.1252  LC_CTYPE=English_Switzerland.1252    LC_MONETARY=English_Switzerland.1252 LC_NUMERIC=C                        
[5] LC_TIME=English_Switzerland.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] extrafont_0.17   hrbrthemes_0.8.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7        pillar_1.7.0      compiler_4.1.3    later_1.3.0       tools_4.1.3       digest_0.6.29     evaluate_0.15     lifecycle_1.0.1  
 [9] tibble_3.1.6      gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.12      DBI_1.1.2         cli_3.1.0         rstudioapi_0.13   writexl_1.4.0    
[17] xfun_0.30         fastmap_1.1.0     Rttf2pt1_1.3.10   stringr_1.4.0     knitr_1.37        systemfonts_1.0.4 gdtools_0.2.4     generics_0.1.2   
[25] vctrs_0.3.8       grid_4.1.3        glue_1.6.0        R6_2.5.1          fansi_0.5.0       RMySQL_0.10.23    pool_0.1.6        rmarkdown_2.13   
[33] farver_2.1.0      ggplot2_3.3.5     purrr_0.3.4       extrafontdb_1.0   magrittr_2.0.1    scales_1.1.1      ggthemes_4.2.4    ellipsis_0.3.2   
[41] htmltools_0.5.2   assertthat_0.2.1  colorspace_2.0-2  labeling_0.4.2    utf8_1.2.2        stringi_1.7.6     munsell_0.5.0     crayon_1.5.0 

Does anyone have an idea on how to fix this issue?

2

There are 2 answers

5
JBGruber On BEST ANSWER

The best solution to my knowledge is showtext and sysfonts. First, add the font to the session:

# directly from google fonts
sysfonts::font_add_google("Roboto Condensed")
# or add an arbitrary font with
sysfonts::font_add("Roboto Condensed", regular = "RobotoCondensed-Regular.ttf")

Once this is done, simply load showtext and run showtext_auto() once to activate it (you need to repeat add_font* and showtext_auto every session):

library(ggplot2)
library(showtext)

showtext_auto()
ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
       title="Seminal ggplot2 scatterplot example",
       subtitle="A plot that is only useful for demonstration purposes",
       caption="Brought to you by the letter 'g'") + 
  hrbrthemes::theme_ipsum_rc()

Created on 2022-03-22 by the reprex package (v2.0.1)

You can also try to make the font available permanently. But it seems to be hit and miss. Theoretically, you need to install the font into C:\Windows\Fonts, which you can do by 1) unzip the fonts, 2) right click and "install for all users". If you install the fonts in a different way, there is a good chance Windows will only put a link into C:\Windows\Fonts, which R can't deal with.

You can check available fonts with:

fonts_df <- sysfonts::font_files()
View(fonts_df)

This works for most fonts in my experience but with Roboto, I still had no luck for some reason. add_font* seems to be the best way to go for now.

0
PTS390 On

I have the same issue, and it is really frustrating As JBGRuber suggested above, it seems that windows renames the font files, but when I use

fonts_df <- sysfonts::font_files()
View(fonts_df)

They all show up.

At any rate, the way I have been playing around this is:

windowsFonts(Robot=windowsFont("Roboto Condensed"))

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
       title="Seminal ggplot2 scatterplot example",
       subtitle="A plot that is only useful for demonstration purposes",
       caption="Brought to you by the letter 'g'") +
  
  theme_bw() +
  theme(text=element_text(family="Robot",    
                          face="bold", 
                          size=12))

enter image description here

Hope this helps