# TikZ and R

December 20, 2011
By

(This article was first published on The PolStat Feed, and kindly contributed to R-bloggers)

TikZ is an awesome macro for producing beautiful graphics in LaTeX. I use it constantly as it has a very intuitive syntax, and it is easy to define global settings for your plots, ensuring that the graphics in your paper are all uniformly styled. One thing that has bothered me is that when I include figures from R in my document this uniform styling is broken. Luckily some clever chaps have written an R package that take any graphical output from R and translates it into LaTeX output in the TikZ format. The library can output standalone .tex documents that just need to be compiled, or it can output LaTeX code ready to be included in your document. The output is saved in your working directory. Below is a comparison between the output from R and the output from tikzDevice:

R output

tikzDevice

The difference is not striking in the above example, but the axis text is now set with the LaTeX computer modern font (or whatever font you have specified in your .tex file), and the lines are finer drawn. The little code snippet below demonstrate how to use the package in combination with ggplot:

To include the output in your latex document simply use the following code in your .tex file: The only downside to using tikzDevice is that if you have many figures in your document (as I have in my thesis) compilation times can be frustratingly long.