library(ggplot2)library(ragg)library(systemfonts)# remark: have to set the chunk output device to "ragg_png"knitr::opts_chunk$set(dev ="ragg_png")
Get fonts
We first need to make the fonts available to R. Either our desired font is already installed on the local system, or we can download one of the many free fonts available on the internet, for example from https://fonts.google.com/.
The systemfonts package helps to list locally installed fonts that R can “see” or make a downloaded font file (typically a TrueType file) available to R.
Code
# list available fonts:fonts <- systemfonts::system_fonts()fonts
# ... register the newly downloaded font for use in Rsystemfonts::register_font(name ="Gochi Hand", plain = ttf)# ... list the now registered fontssystemfonts::registry_fonts()
# A tibble: 4 × 7
path index family style weight italic features
<chr> <int> <chr> <chr> <ord> <lgl> <list>
1 /var/folders/qm/f4lwc7c10193k2zfy4s… 0 Gochi… Regu… normal FALSE <int>
2 /var/folders/qm/f4lwc7c10193k2zfy4s… 0 Gochi… Bold bold FALSE <int>
3 /var/folders/qm/f4lwc7c10193k2zfy4s… 0 Gochi… Ital… normal TRUE <int>
4 /var/folders/qm/f4lwc7c10193k2zfy4s… 0 Gochi… Bold… bold TRUE <int>
Plot
With the fonts available to R, we can now create plots that use custom fonts for some or all of the text elements. Fonts are defined by element_text() using the arguments family (see the family column returned by systemfonts functions) and face (one of “plain”, “italic”, “bold” or “bold.italic”, see style column return by systemfonts functions).
Code
# remember that this will only work if you have# activated the `ragg` backend to render the plots# create base plotp0 <-ggplot(data = mycars, mapping =aes(x = cyl)) +coord_cartesian(ylim =c(0, 250)) +labs(x ="Number of cylinders", y ="Gross horsepower") +theme_bw(20) +theme(panel.grid =element_blank())# ... add bars and Roman fontp0 +geom_col(data = mycars_summary, mapping =aes(y = hp_avg),fill ="gray70") +geom_errorbar(data = mycars_summary,mapping =aes(ymin = hp_avg - hp_sem,ymax = hp_avg + hp_sem),width =0.6) +annotate("text", x =-Inf, y =Inf, hjust =-0.1, vjust =1.1,label ="Question:\nIs this font well readable?",family ="Z003", size =18/ .pt) +theme(text =element_text(family ="Z003"))
The page at https://r-graph-gallery.com/custom-fonts-in-R-and-ggplot2.html describes two ways to use custom fonts with ggplot2:
ragg is a graphics backend (it renders the plot to a bitmap graphics device like png()), and can make all system fonts available to be used in R. This is the approach used above. ragg is based on the fast AGG library, does not require you to use any other packages and is also fully integrated into RStudio (you can select is as your default graphics device in “Tools” -> “Global options…” -> “General”, panel “Graphics”).
showtext is a package that is especially designed to use custom fonts with pdf() devices. After loading a font and activating showtext, it renders the characters as filled polygons.
Another useful page with more detailed information about using fonts in ggplot2 and R in general: https://yjunechoe.github.io/posts/2021-06-24-setting-up-and-debugging-custom-fonts/