Control font for annotation in ggplot2

Viewed 920

I've a plot and I've used annotate to add some indications.

However, it is not using the general font used for the theme: Roboto, from hrbrthemes.

library(tidyverse)
library(hrbrthemes)

ggplot(diamonds, aes(x = carat, y = y)) + geom_col() +
  theme_ipsum_rc(grid="XY") +
  labs(title = "My plot title using Roboto") +
  annotate(
    geom = "text", x = 3, y = 10000, 
    label = tt, hjust = 0, vjust = 1, size = 6,
    family= "Roboto", 
    size= theme_get()$text[["size"]]/2.5, 
    fontface="italic")

I've tried to set the family font in the annotate function call.

Other way would be to use:

family= theme_get()$text[["family"]]

So the annotation gets the font family from the general plot (Roboto), but this is giving me:

Warning messages:
1: In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database
2: In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database
3: In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  :
  font family not found in Windows font database
4: Duplicated aesthetics after name standardisation: size 

Howcome is this possible if the title is using the Roboto font?

enter image description here

UPDATE 1:

Have installed extrafont library, imported the fonts and registered them:

font_import()
fonts()
loadfonts()

But still cannot set the font for annotate function. all fonts are registered:

font_import()
C:\Windows\Fonts\Roboto-Black.ttf : Roboto-Black already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-BlackItalic.ttf : Roboto-BlackItalic already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-Bold.ttf : Roboto-Bold already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-BoldCondensed.ttf : Roboto-BoldCondensed already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-BoldCondensedItalic.ttf : Roboto-BoldCondensedItalic already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-Italic.ttf : Roboto-Italic already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-Light.ttf : Roboto-Light already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-LightItalic.ttf : Roboto-LightItalic already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-Medium.ttf : Roboto-Medium already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-MediumItalic.ttf : Roboto-MediumItalic already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-Regular.ttf : Roboto-Regular already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-Thin.ttf : Roboto-Thin already registered in fonts database. Skipping.
C:\Windows\Fonts\Roboto-ThinItalic.ttf : Roboto-ThinItalic already registered in fonts database. Skipping.

loadfonts()

Roboto already registered with pdfFonts().

Update 2:

Just with plain ggplot2, I cannot set the font for annotate neither.

ggplot(diamonds, aes(x = carat, y = y)) + geom_col() +
  labs(title = "My plot title using Roboto") +
  annotate(
    geom = "text", x = 3, y = 10000, 
    label = tt, hjust = 0, vjust = 1, size = 6,
    family= "Roboto")

Gives:

Warning message: In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : font family not found in Windows font database

Plots renders with another font in the annotation

0 Answers
Related