rggplot2showtext

How to use the dalek font in R plots?


I want to use the dalek font in my R plots. I tried to use the showtext plot, but I had no success. Can someone provide some guidance?

enter image description here


Solution

  • It turns out that it was necessary to download a regular version of font. Use the function font_add and then use the showtext_auto function to use new fonts in the plots. A small example:

    library(latex2exp)
    library(showtext);
    font_add_google("Gochi Hand", "gochi")
    font_add_google("Schoolbell", "bell")
    font_add_google("Covered By Your Grace", "grace")
    font_add_google("Rock Salt",
    "rock")
    sysfonts::font_add(family = "dalek", # Name you want to use 
                       regular = "fonts/Dalek.otf") # Text of the 'General' tab plus the font
    sysfonts::font_add(family = "ancient-helenic", # Name you want to use 
                       regular = "fonts/ancienthellenic-webfont.ttf") # Text of the 'General' tab plus the font 
    showtext_auto()
    
    versicolor <- which(iris$Species=="versicolor")
    x <- iris$Sepal.Length[versicolor]
    y <- iris$Petal.Length[versicolor]
    
    showtext_opts(dpi = 96);
    mod <-  lm(y ~ x)
    op <-  par(cex.lab = 2, cex.axis = 1.5, cex.main = 2)
    plot(x, y, pch = 16, col = "steelblue", xlab = "Sepal Length", ylab = "Petal Length", family = "gochi");
    grid()
    title("Petal Length regressed on Sepal Length", family = "bell")
    text(5.85, 5.1, "Outlier?", cex = 2, col = "steelblue", family = "grace")
    abline(coef(mod))
    abline(a = .2, b = .7, col = "red")
    par(family = "rock")
    text(5.2, 4, c(TeX("True model: $\\mu_Y=\\beta_0 +\\beta_1 X$")), cex = 1.5, col = "red", srt = 20, family="ancient-helenic")
    text(6.3, 3.9, bquote(paste("OLS: ", hat(y) == .(unname(round(coef(mod)[1],3))) * x + .(unname(round(coef(mod)[2],3))))), cex = 1.5, srt = 15, family ="dalek")
    legend("topright", legend = c("Truth", "OLS"), col = c("red", "black"), lty = 1)
    par(op)
    

    enter image description here