rggplot2fonts

font family not found in Windows font database


when I use rstudio in win11, I cannot use custom fonts for ggplot, I look previous question about this in Stack Overflow but none of them help me. I run a simple code:

ggplot(mtcars, aes(hp, mpg)) +
  geom_point() +
  theme(text = element_text(family = "Noto Mono"))

and these warnings shown in Console:

> warnings()
Warning messages:
1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
4: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
5: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
6: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
7: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
8: In grid.Call.graphics(C_text, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
9: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
10: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
11: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
12: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
13: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
14: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
15: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database
16: In grid.Call.graphics(C_text, as.graphicsAnnot(x$label),  ... :
  font family not found in Windows font database

How to fix this?
I want to use Vazirmatn for adding some Persian text as label, title & etc. I installed Vazirmatn & Noto Mono in my device, none of my installed font works with element_text(family = "my_font_family")


Solution

  • did you use extrafont library?
    You can change the font of ggplot function in R by using extrafont package1. Here are the steps:

    Install extrafont package using this command:
    install.packages("extrafont")
    Load extrafont package:
    library(extrafont)
    Import Vazirmatn font:
    font_import(pattern = "Vazirmatn")
    Register Vazirmatn font:
    loadfonts(device = "win")
    Use Vazirmatn font in ggplot function:

    ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point() +
        theme(text=element_text(family="Vazirmatn"))