Search code examples
rggplot2fonts

How do I install icons to add to ggplot2 title in a quarto file?


I am having issues trying to implement the following solution (from How to add icons to ggplot captions and titles?) in a quarto file.

library(ggplot2)
library(ggtext)
library(showtext)
#> Loading required package: sysfonts
#> Loading required package: showtextdb
library(ggrepel)
library(cowplot)

font_add_google("Martel", family = "title")
font_add_google("Libre Caslon Display", family = "subtitle")
font_add_google("Space Mono", family = "axis")
font_add_google("Spartan", family = "caption")

fa_path <- systemfonts::font_info(family = "Font Awesome 6 Brands Regular")[["path"]]
font_add(family = "fa-brands", regular = fa_path)

showtext_auto()

ggplot(babynames, aes(x = year, y = prop)) +
  geom_step(size = 0.5, show.legend = FALSE, direction = "hv") +
  theme_bw() +
  labs(
    x = "Year",
    y = "Proportion",
    title = "How Unique are Names in the United States?",
    subtitle = "This visualization illustrates the proportion of most given baby names in that year between 1880 and 2017",
    caption = "Source: {babynames} package | Plot: <span style='font-family: \"fa-brands\"'>&#xf09b;</span> muhammetozkrca | TidyTuesday-Week 12"
  ) +
  background_grid(major = "none", minor = "none") +
  theme(
    plot.title = element_text(hjust = 0.5, family = "title", size = 20),
    plot.subtitle = element_markdown(hjust = 0.5, family = "subtitle", size = 14),
    plot.caption = element_markdown(hjust = 0.5, size = 14),
    legend.position = c(0.9, 0.6),
    legend.justification = "center",
    legend.title = element_text(family = "caption", hjust = 1, vjust = 0.7),
    legend.title.align = 0.5,
    axis.title.x = element_text(family = "axis"),
    axis.title.y = element_text(family = "axis"),
    panel.border = element_blank(),
    axis.ticks = element_blank()
  )

As the answer points out, I need to call the font awesome fonts via

fa_path <- systemfonts::font_info(family = "Font Awesome 6 Brands Regular")[["path"]]
font_add(family = "fa-brands", regular = fa_path)

I can't get this to work. Through other post suggestions I've tried installing the Font Awesome 6 Brands-Regular-400.otf (from the desktop verstion) as well as fa-brands-400.ttf (from the web version). I've run them separately as they are named the same.

Trying to trouble shoot I've noticed that

fa_path <- systemfonts::font_info(family = "fa-brands-400")[["path"]]
fa_path <- systemfonts::font_info(family = "Font Awesome 6 Brands Regular")[["path"]]

both return "C:\\WINDOWS\\Fonts\\arial.ttf", which leads me to believe the calls aren't even pointing to the correct files.


Solution

  • I believe the font family should be Font Awesome 6 Brands (I have only installed otf fonts from Desktop). If you are able to switch to AGG graphics device (must install ragg package first), font handling and access simplifies quite a lot.

    Knitr should use ragg_png for Quarto too, this can be configured through knitr:opts_chunkin YAML block:

    knitr:
      opts_chunk:
        dev: ragg_png
    

    To access certain font variants (e.g. use FA Solid instead of FA Regular), we can first use register_variant().

    ggtext::geom_richtext() with Font Aweseome, registering fa-solid font family to access FA Solid icons and using the same fa-solid in ggplot titles:

    # reprex must use ragg_png, otherwise FA fonts are not found
     
    #+ setup, include=FALSE
    knitr::opts_chunk$set(dev = "ragg_png")
    
    #+ main
    library(systemfonts)
    library(dplyr)
    library(ggplot2)
    library(ggtext)  
    
    reset_font_cache()
    
    # list Font Awsome fonts
    system_fonts() %>% 
      filter(grepl("Awesome", family )) %>%
      glimpse()
    #> Rows: 3
    #> Columns: 9
    #> $ path      <chr> "C:\\Users\\marguslt\\AppData\\Local\\Microsoft\\Windows\\Fo…
    #> $ index     <int> 0, 0, 0
    #> $ name      <chr> "FontAwesome6Brands-Regular", "FontAwesome6Free-Regular", "F…
    #> $ family    <chr> "Font Awesome 6 Brands", "Font Awesome 6 Free", "Font Awesom…
    #> $ style     <chr> "Regular", "Regular", "Solid"
    #> $ weight    <ord> normal, normal, heavy
    #> $ width     <ord> normal, normal, normal
    #> $ italic    <lgl> FALSE, FALSE, FALSE
    #> $ monospace <lgl> FALSE, FALSE, FALSE
    
    system_fonts() %>% 
      filter(grepl("Awesome", family )) %>%
      mutate(path = basename(path)) %>% 
      select(-c(index, family))
    #> # A tibble: 3 × 7
    #>   path                                 name  style weight width italic monospace
    #>   <chr>                                <chr> <chr> <ord>  <ord> <lgl>  <lgl>    
    #> 1 Font Awesome 6 Brands-Regular-400.o… Font… Regu… normal norm… FALSE  FALSE    
    #> 2 Font Awesome 6 Free-Regular-400.otf  Font… Regu… normal norm… FALSE  FALSE    
    #> 3 Font Awesome 6 Free-Solid-900.otf    Font… Solid heavy  norm… FALSE  FALSE
    
    # register new family to access "Font Awesome 6 Free-Solid-900.otf"
    register_variant(
      name = "fa-solid", 
      family = "Font Awesome 6 Free", 
      weight = "heavy"
    )
    
    ggplot() + 
      geom_richtext(aes(x = 0, y = 1, 
                        label = "rich <span style='font-size:60pt; 
                        font-family: \"Font Awesome 6 Brands\"'>github</span>
                        text<br>Font Awesome 6 Brands")) +
      geom_richtext(aes(x = 1, y = 1, 
                        label = "rich <span style='font-size:60pt; 
                        font-family: \"Font Awesome 6 Free\"'>chart-bar</span>
                        text<br>Font Awesome 6 Free <br>Regular")) +
      geom_richtext(aes(x = 2, y = 1, 
                        label = "rich <span style='font-size:60pt; 
                        font-family: \"fa-solid\"'>chart-simple</span>
                        text<br>Font Awesome 6 Free <br>Solid")) +
      # coord_fixed(ratio = 2) +
      scale_x_discrete(expand = expansion(mult = .3), name = "gear") +
      scale_y_discrete(expand = expansion(mult = .2), limits = c(0,2), name = "bars") +
      coord_fixed(ratio = 2) +
      ggtitle("globe city compass") +
      theme_minimal() +
      theme(title = element_text(family = "fa-solid", size = 20))
    #> Warning: Continuous limits supplied to discrete scale.
    #> ℹ Did you mean `limits = factor(...)` or `scale_*_continuous()`?
    

    Created on 2023-05-24 with reprex v2.0.2