TikZ diagrams with R: tikzDevice

There are several options for integrating your R workspace with LaTeX. One of these is the R package tikzDevice that allows you to export images created in R as tikz code in a .tex file, for immediate use in a LaTeX document via the line \include{diagrams}.

A simpler way, the one we all start out with, is to export an image from R as a .pdf, then include it using the line \includegraphics{diagrams.pdf}. This is a pretty easy and straightforward workflow – so, why would I want to use tikzDevice?

There several advantages to converting your images into TikZ code directly from R:

1. TikZ diagrams consist of vectors coded directly into your LaTeX document: there’s no loss of image resolution.
2. The labels on TikZ diagrams match the font of your LaTeX document.
3. Wonderful LaTeX equations can be effortlessly used as labels in your diagrams.
4. You can harness the power of the loop in R to create a single .tex file containing many images.
5. You can harness the power of the loop in R to add \caption{} and \label{} lines to all your images for immediate reference within LaTeX.
6. You can include all these features and output via one line in LaTeX: \include{diagrams}.

A Simple Example

That being said, let’s export a TikZ scatterplot using the tikzDevicepackage. We will use data posted on Dr. Walter Enders web site.

Notice the fancy latex equations as labels on the plot.

R:

# gdata helps read .xls files
require(gdata)
df = read.xls("http://cba.ua.edu/assets/docs/wenders/arch.xls", sheet = 1)

# tikzDevice will export the plots as a .tex file
require(tikzDevice)

# choose a name and path for the .tex file
# folder should be the same as where your latex document resides
tikz( '/Users/kevingoulding/latex_documents/thesis/plot_with_line.tex' )

plot(df, xlab = "$\\alpha_t + \\hat{\\beta}X_t$", ylab = "$Y_t$",
main = "$Y_t = \\alpha_t + \\hat{\\beta}X_t$")
abline(h = mean(df[,2]), col = "red", lwd = 2)

dev.off()					# must turn device off to complete .tex file


To include this diagram in your LaTeX document, simply add the line \include{plot_with_line} and compile. Don’t forget to include \usepackage{tikz} in the preamble. If you zoom in, you can see that we’ve labeled the plot and axes using LaTeX math language (amsmath).

A few things to be careful with as you try to code LaTeX equations from within R:

• All backslashes need to be doubled. \ –> \\.
• All equations still need to be bordered by \$ on each side.

To be continued…

Tags: , ,

5 Responses to “TikZ diagrams with R: tikzDevice”

1. Thank you very much for these instructions. I encountered two problems however.

One, when I inserted a TikZ picture in my document with this method it was way too big and extended beyond the visible page, I had to add a ‘scale=0.5’ option to ‘begin\{tikzpicture}[*options here*]’ to make it fit within the margins. This worked, but surely there must be a more intelligent way to make the picture fit inside the margins?

Two, you recommend using \include to add the tex file with the tikz picture in a document. However, this made LaTeX only put one picture on a page, the second TikZ picture would be placed on a new page even though it was obvious there was enough space for two. According to http://tex.stackexchange.com/questions/246/when-should-i-use-input-vs-include this is caused by \include adding page breaks, something which is not done by \input. I tried this and \input did not give me this problem.

• Hi Alexander, If I remember correctly I simply played with the scale parameter until it looked ‘right’ on the page. Unfortunately this is obviously not ideal but worked for me. In general, I had scale set around 0.5 for most of my diagrams.

As for /include versus /input, thanks for the heads up. I’ve never been a huge fan of how latex ‘smartly’ chooses a diagram’s location on the page. Hope this helps. -Kevin

2. Now that I have finished my master thesis which used tikzDevice, I have some better advice to give after spending a lot of time to figure out a better method. You can pass the options ‘width’ and ‘height’ to tikzDevice, with specify width in inches. This looks noticeably better than scaling, I found that the following image size is good for A4 paper: tikz(“.tex”, width=3.5, height=3.5)

Regarding placement of figures, in my thesis I had to place six figures for TiKZ images in a sequence without interruption by text. I used commands like \begin{figure}[t!] and \begin{figure}[b!] to place them at either the top or bottom of the page. Every figure was 3,5 inch wide and 3.5 inch high so that two could fit on a page. After giving all six commands to place the figures I gave the command \clearpage so that all figures were forced to be printed. That also creates a page break after printing them, but that was no issue because two figures occupy the entire page anyway. This placed six figures on three pages and worked nicely for me.

Some time next week I will publish my master thesis along with sources and R scripts for tikzDevice on my blog, so anyone will be able to study them.

• Alexander, thanks for your comments and insight into improving the formatting & output of tikzDevice. I look forward to reading your posts on the matter! Cheers, Kevin