Search code examples
rggplot2font-awesomeextrafont

How to use FontAwesome Free Font in ggplot


I try to plot a certain glyph using extrafont (on Windows 10):

library(extrafont)
library(ggplot2)
loadfonts()

ft <- fonttable()

ft[grepl("Awesome", ft$FontName), c("FullName", "FamilyName", "FontName")]

#                          FullName                    FamilyName                   FontName
# 367 Font Awesome 5 Brands Regular Font Awesome 5 Brands Regular FontAwesome5Brands-Regular
# 368     Font Awesome 5 Free Solid     Font Awesome 5 Free Solid     FontAwesome5Free-Solid

ggplot() +
 geom_label(aes(x = 1, y = 1,  label = "\uf0f3"), 
 family = "Font Awesome 5 Free Solid", size = 16) +
 theme_minimal()

This works as expected:

ggplot with a FontAwesome Bell symbol

However, if I try to use another Unicode I just get an empty box:

ggplot() +
 geom_label(aes(x = 1, y = 1,  label = "\uf1fd"), 
 family = "Font Awesome 5 Free Solid", size = 16) +
 theme_minimal()

ggplot with an empty box instead of a FontAwesome symbol

For all it is worth, I am using this FontAwesome CheatSheet to get the Unicode hex values.

Why is it not working?

Update

@Pedro was suggetsing in the comments that it may have something to do with the Fontawesome version, so I looked up the v4 Unicodes, here's what I got:

library(rvest)
library(stringr)
library(purrr)
library(tibble)
library(dplyr)
library(ggplot2)
library(extrafont)

doc <- "https://fontawesome.com/v4/cheatsheet/" %>% 
  read_html()

unicodes <- doc %>% 
  html_elements(".row .fa+span") %>% 
  html_text2() 

fa <- unicodes %>% 
  str_subset(fixed("(alias)"), TRUE) %>% 
  str_extract("[0-9a-f]+") %>% 
  paste0("0x", .) %>% 
  map_chr(intToUtf8) %>% 
  tibble(label = .) %>% 
  mutate(x = (seq_along(label) - 1) %/% ceiling(sqrt(length(label)))) %>% 
  group_by(x) %>% 
  mutate(y = seq_along(label) - 1)

ggplot(fa) +
  geom_text(aes(x, y,  label = label), 
             family = "Font Awesome 5 Free Solid", size = 3) +
  geom_text(aes(x, y,  label = label), 
            family = "Font Awesome 5 Brands Regular", size = 3, color = "red") +
  
  theme_void()

A plot with all fontawesome glpyhs found on the cheatsheet. We see that several of the glyphs are not rendered

Session Info

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

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

other attached packages:
[1] ggplot2_3.4.0  extrafont_0.18

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9       bslib_0.4.2      compiler_4.2.2   pillar_1.8.1    
 [5] later_1.3.0      jquerylib_0.1.4  tools_4.2.2      digest_0.6.31   
 [9] jsonlite_1.8.4   lifecycle_1.0.3  tibble_3.1.8     gtable_0.3.1    
[13] pkgconfig_2.0.3  rlang_1.0.6      DBI_1.1.3        shiny_1.7.3     
[17] cli_3.5.0        fastmap_1.1.0    Rttf2pt1_1.3.8   withr_2.5.0     
[21] dplyr_1.0.10     generics_0.1.3   sass_0.4.4       vctrs_0.5.1     
[25] tidyselect_1.2.0 grid_4.2.2       glue_1.6.2       R6_2.5.1        
[29] fansi_1.0.3      extrafontdb_1.0  magrittr_2.0.3   scales_1.2.1    
[33] promises_1.2.0.1 ellipsis_0.3.2   htmltools_0.5.4  assertthat_0.2.1
[37] mime_0.12        xtable_1.8-4     colorspace_2.0-3 httpuv_1.6.6    
[41] utf8_1.2.2       munsell_0.5.0    cachem_1.0.6

Solution

  • After uninstalling all Font Awesome Fonts from my machine and re-installing them from the source, all glyphs could be rendered properly.

    I just needed to install the webfonts (not the desktop version), because only the webfont comes in ttf format, while the desktop only ships the otf which cannot be read by extrafont::font_import.