As I mentioned two and a half year ago it’s a little tricky to use otf-fonts in ggplot-graphics in LaTeX-documents.

When you are use XeLaTeX instead of pdfladtex you have to use another option:

tikz-Device

With the tikz-device you can use the font you’re using in the document within your ggplot.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
\documentclass{article}
\usepackage{graphicx}
\usepackage{fontspec}
\usepackage{tikz}
 
    \setmainfont[ItalicFont={Source Code Pro Italic},
                 BoldFont={Source Code Pro Bold},
                 BoldItalicFont={Source Code Pro Bold Italic},
    ]{Source Code Pro}
 
\begin{document}
  <<set-options>>=
 Sys.setenv(LANG = "en")  
 options(tikzDefaultEngine='xetex')
 library(ggplot2, quietly=TRUE)
@
 
<<message=FALSE, echo=FALSE, dev='cairo_pdf', warning=FALSE>>=
ggplot(mtcars, aes(x=wt, y=mpg, color=factor(gear))) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(family="Source Code Pro"))
@
\end{document}

But there’s also a negative part. It feels really slow generating the final pdf.