TikZ and R

December 20, 2011

(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


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.

To leave a comment for the author, please follow the link and comment on their blog: The PolStat Feed.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)