1 min read

Latex Font in a ggplot

First one needs to go to the link below and download the TTFs of latin modern roman (standard latex font).

For mac: click on each ttf file and click install. For windows: copy paste files to C:/windows/fonts

then run font_import(pattern = “lmroman*“).

library(extrafont) 
library(tidyverse)



# https://www.andrewheiss.com/blog/2017/09/27/working-with-r-cairo-graphics-custom-fonts-and-ggplot/
# link www.fontsquirrel.com/fonts/latin-modern-roman

# execute once to add fonts:
# font_import(pattern = "lmroman*") 



loadfonts()
data <- data.frame(x=1:10, y=1:10)
p <- data %>% ggplot(aes(x, y)) + geom_point() + 
    theme(text         = element_text(size=10, family="LM Roman 10", face = "italic")) + 
  ggtitle("for the boys")
p + theme(plot.subtitle = element_text(vjust = 1), 
    plot.caption = element_text(vjust = 1)) +labs(subtitle = "lil subtitle i guess", 
    caption = "Source: idk, 2018")
Example 1

Figure 1: Example 1

data(mtcars)
p2 <- mtcars %>% ggplot(aes(x=mpg, y=disp)) + geom_col(aes(col=cyl)) + 
   theme(text         = element_text(size=10, family="LM Roman 10")) + 
  ggtitle("for the boys")

p2 
Example 2

Figure 2: Example 2