rggplot2ggrepel

change font familly in geom_text_repel()


Summary

I would like to use AvantGarde font family in a geom_text_repel but I get an error.

Minimal code example

library(ggrepel)
p <- ggplot(mtcars,
            aes(wt, mpg, label = rownames(mtcars), colour = factor(cyl))) +
  geom_point()

p + geom_text_repel(family = 'AvantGarde')

Here is the error message I get output produced by the code:

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  polygon edge not found
In addition: There were 12 warnings (use warnings() to see them)

with the following warnings:

1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  no font could be found for family "AvantGarde"

Version information

Here is the output from sessionInfo() in my R session:

R version 4.1.3 (2022-03-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 13.2.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] ggrepel_0.9.2.9999 ggplot2_3.4.1     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.10      fansi_1.0.4      withr_2.5.0      dplyr_1.1.0     
 [5] utf8_1.2.3       grid_4.1.3       R6_2.5.1         lifecycle_1.0.3 
 [9] gtable_0.3.3     magrittr_2.0.3   scales_1.2.1     pillar_1.9.0    
[13] rlang_1.1.0      cli_3.6.1        farver_2.1.1     rstudioapi_0.14 
[17] generics_0.1.3   vctrs_0.6.1      labeling_0.4.2   tools_4.1.3     
[21] glue_1.6.2       munsell_0.5.0    compiler_4.1.3   pkgconfig_2.0.3 
[25] colorspace_2.1-0 tidyselect_1.2.0 tibble_3.2.1 

I tried all the other types of fonts available in R without success


Solution

  • I found a solution here: (Fonts not available in R after importing)

    You can modify the Graphic Device for RStudio to AGG and work with the fonts in a seamless way. Just like the default ones (Changing just the "family" on theme()). Just install the ragg package and follow:

    Tools > Global options > General > Graphics > Backend: AGG

    Documentation: https://ragg.r-lib.org/