The marriage between R and LaTeX – Part 1

In this article, I will be discussing how R and LaTeX integrate with each other. This is a Part 1 of many articles [Part 1, Part 2, Part 3]

In particular, this article will discuss how to include R plots into LaTeX documents. Next article – Part 2 – will expand further on what is covered in this article. Part 3 will discuss how to include any type of R Table into LaTeX and HTML documents through the R xtable package.

Hope you enjoy, I am happy to hear your comments.

There are 2 sides here that you need to do: your LaTeX document side and your R IDE side. You need to have LaTeX installed on your system.

If you are new to LaTeX, there are different distributions that you need to install like TeX Live (which is the best in my experience and is convenient for both Linux and Windows OS) or MacTex (for Mac OS). There are others of course like MikTeX (Windows),  proTeXt (windows) … and some even don’t require installation at all as they are on-line services.

I would assume in this article that you have enough knowledge in both R and LaTeX.

I will be using in the tutorial and other articles in this series [Part 2, Part 3]:

  • For R I will be using R 3.3.3 (Another Canoe) and RStudio.
  • On the LaTeX side, I have a complete TeX Live installation on my Linux Fedora 24. I am using TeX Maker as a LaTeX editor.

Required packages to install in R for this Tutorial

  • xtable package – allows you to create LaTeX tables from inside R. It allows you also to do the same for HTML (not covered in this tutorial). In other words, transform any possible R table into a LaTeX table. You install it by issuing the command in RStudio: install.packages(“xtable”)
  • tikzDevice package – allows you to generate .tex files from the R environment. You install it by issuing the command in RStudio:   install.packages(“tikzDevice”)

There are other packages that do different functions and that allow you to integrate R tables into LaTeX documents. You can check this link and this link for more techniques.

In my experience, the R packages xtable and tikzDevice do the job very well and they are easy to work with.

Required packages to install in LaTeX for this Tutorial

  • The LaTeX tikz package. You should include it as \usepackage{tikz} in the document header or preamble.

Add also in the document header of your LaTeX document: \usetikzlibrary{positioning,shadows.blur}

I will be using material from the reference guides of the packages mentioned above which can be found:

  • The R xtable package reference manual can be found here.
  • The R tikzDevice package reference manual can be found here.
  • For the  LaTeX tikz package I used examples from Jacques Cremer  guide ” A very minimal introduction to TikZ” found here.

R plots into LateX documents

You need to know first the width of your LaTeX document. You might be writing a paper with 2 columns style. You need to know the column width in inches. In the majority of the cases, the column width would be 7 inches but that might differ in your case.

Let’s say I have the following plot in R. Using the mtcars dataset (comes by default in R).

counts <- table(mtcars$gear)
barplot(counts, main=”Car Distribution”, xlab=”Number of Gears”)

car distribution R plot

To insert this plot into a LaTeX document, I would do the following in RStudio:

I would load first the tikzDevice R library by issuing the following R command {PS: I would assume you have installed the package: install.packages(“tikzDevice”)}


counts <- table(mtcars$gear)

tikz(file = ‘mybarplot.tex’, width = 6, height = 6)
barplot(counts, main=”Car Distribution”, xlab=”Number of Gears”)

The width and height of the output figure are in inches. This is why I mentioned previously the need to know the width of the column in your LaTeX document. You can include the full path of the output file like file=’/home/hb/barplot.tex’. If you don’t specify a path the file will be generated in your RStudio working directory.

There are many ways to change the working directory of your RStudio which I advice you do in the beginning and it is more convenient to set it to your LaTeX document directory.  You can issue per example the following R command If you are on a Linux:


You can also do this by GUI by going in RStudio to Session -> Set Working Directory -> Choose Directory.

Enough for that!!! let us return back to the topic but make sure you know in which directory the .tex file is generated.

You should always use dev stand for device. In R a device is a mechanism to produce a plot. In other words, this device could be many things. It could your screen where your plots are shown in front of you in RStudio. You could decide to have as a device  a pdf file, a  png or a tiff image etc…

When you say you are telling R to  close the device/file that I already opened. Well which one? It is none than the tikz device you already just opened by the tikz function you already put before your R plot commands. If you don’t specify this , R will consider whatever afterwards as part of the tikz device. Well this is sometimes needed in case you want to produce more than one R plot and put them in the .tex file.

Actually tikz is nothing but another plotting device like pdf(filename, width=9, height=9) or like png(file, width=300, height=300).

Now I sometime find myself needing to use the R par() function. It is a lovely function that a lot of people are afraid of.

par() stands for parameters. If you issue a par() on your R terminal you can obtain many parameters (first they will default ones) for any plot you do in R.

par(mar=c(3, 3.5, 1, 0.4), mgp=c(2,0.2,0))

As shown above the mar() function stands for margins of the plot (same as the concept of margins of an MS Word document). It takes 4 parameters: the first being the bottom margin, then left, then up then right margin. The next function mgp is for controlling the places of xlab, ylab … I find this page useful for more info.

Why all this? Well from my experience, you need to include the par function with its parameters inside the tikz(….) block to tweak your plot dimensions, xaxis …. so that it appears as you wanted it to appear in your LaTeX document.

counts <- table(mtcars$gear)

tikz(file = ‘mybarplot.tex’, width = 6, height = 6)
par(mar=c(3, 3.5, 1, 0.4), mgp=c(2,0.2,0))
barplot(counts, main=”Car Distribution”, xlab=”Number of Gears”))

Caution- Escaping LaTeX characters problem

I used the following R code for one of my plots. I had in it a very important problem (The following code will produce errors):

tikz(file = ‘BarplotHourSpent.tex’, width = 2.5, height = 2.5)
par(mar=c(3, 2.7, 1, 0.2), mgp=c(2,1,0))
barplot((table(hours)/sum(table(hours))) *100, main = “”, ylab = “Percentage (%)”, width = 20,space = 0.1, las = 3, xaxt = “n”, ylim = c(0,60), col=c(“red”,”green”,”yellow”,”blue”))
legend(“topleft”, legend= c(“less than 7 hours”, “between 14 and 27 hours”,”between 28 and 35 hours”,”more than 35 hours”), fill=c(“red”,”green”,”yellow”,”blue”), cebareBonesx = 0.65)

The problem is escaping LaTeX characters. The % is a nasty one. As you probably know the % has a special meaning in LaTeX. Anything that has a special meaning in LaTeX needs to be escaped or resolved (the way you normally do in LaTeX). The escaping should happen inside R code itself. The solution  is to put two backward slashes \\%. Why two \? well one for R and One for LaTeX. The slash needs to be escaped for R and LateX needs to escape the %. Solution would be as follows

tikz(file = ‘BarplotHourSpent.tex’, width = 2.5, height = 2.5)
par(mar=c(3, 2.7, 1, 0.2), mgp=c(2,1,0))
barplot((table(hours)/sum(table(hours))) *100, main = “”, ylab = “Percentage (\\%)”, width = 20,space = 0.1, las = 3, xaxt = “n”, ylim = c(0,60), col=c(“red”,”green”,”yellow”,”blue”))
legend(“topleft”, legend= c(“less than 7 hours”, “between 14 and 27 hours”,”between 28 and 35 hours”,”more than 35 hours”), fill=c(“red”,”green”,”yellow”,”blue”), cex = 0.65)

Other parameters of tikz function are explained below:

  • bg: you can specify a starting background colour for your R plot. The default is “transparent”
  • fg: you can specify a starting foreground colour for your R plot. The default is “black”.
  • standAlone: I advice you not to include this as the default is convenient for you in most cases.  If you are not sure that this effect is needed, don’t bother with this. This parameter specifies whether the output file should be suitable for direct processing by LaTeX. The default is FALSE which what you probably want. FALSE means the .tex file that you are generating, you want it to be included in a larger LaTeX document (which would be your paper).
  • bareBones: That is really important. If you set it to TRUE, the figure will not be wrapped by a LaTeX tikzpicture block of code (which I will discuss in Part 2). This is useful if you want to embedd one TikZ picture into another. Default is FALSE meaning if you open the generated .tex file you will see a huge block of LaTeX tikzpicture block code describing to LaTeX your R Plot.
  • ….

From my experience, I don’t think you need to be concerned by the other parameters that tikz function offer.

Now on the LaTeX side:

First don’t forget to include in the header of your LaTeX:


To include the content of your .tex file which contains the LaTeX equivalent instructions of your R Plot. A simple method is to include it in LaTeX figure block:

\caption{This is my first barplot from R in LaTeX! Yupi!}
From my experience don’t include the .tex extension in the \input command, it is  bad!!!
It happens that some time you need in a paper to include a complete page of plots. Normally it is done by having a hidden LaTeX table without borders where in each cell of this table you include an image.
Suppose you generated 6 R plots and you want to include them in a table? How do you do that? You would use the subfigure package.
The following LaTeX code is a good example.
\caption{Participants demographics}
\begin{subfigure}{0.4\textwidth}\centering \include{graphs/barplotGender} \caption{Gender distribution of participants} \label{fig:genderdistribution} \end{subfigure}
& \begin{subfigure}{0.4\textwidth}\centering \include{graphs/HistogramAgeDist} \caption{Age distribution of participants} \label{fig:agedistribution} \end{subfigure} \\
\begin{subfigure}{0.4\textwidth}\centering \include{graphs/BarplotEyeCondition} \caption{Eye Condition of Participants} \label{fig:EyeCondition} \end{subfigure} & \begin{subfigure}{0.4\textwidth}\centering \include{graphs/BarplotEyeCorrection} \caption{Barplot showing whether participants needed Eye correction (glasses, lenses etc…)} \label{fig:EyeCorrection} \end{subfigure} \\
\begin{subfigure}{0.4\textwidth}\centering \include{graphs/BarplotScreenType}       \caption{Primary screen type used by participants to view 3D content} \label{fig:ScreenType} \end{subfigure}& \begin{subfigure}{0.4\textwidth}\centering \include{graphs/HistogramScreenSize} \caption{Primary screen sizes used by participants to view 3D content} \label{fig:Screensize} \end{subfigure} \\
What you are doing here  is you are using the following block with your \include of the .tex file. “barplotGender”, “HistogramAgeDist” etc.. are all tex files which contains R plots.
There is also another common way of including many images by using LaTeX minipages.

Caveats you might discover & a very useful tweak for graphs

Although the resolution of the graphs or images that are in the form of tikz are just awesome! Well it is a drawing language! you might be in a situation where you want to use an image file type (.png, .jpeg…) or a PDF version of the tikz graph.

I found from my experience that the generated .tex files from R are not suitable on their own even when you tweak the tikz parameters on the LaTeX side or even the plot parameters on the R side. I will be explaining how to tweak the tikz pictures in the next article (section: Important keys to tweak of \tikzpicture for R plots).

The most convenient size of a tikz picture is achieved by including the following in the beginning & the end of the .tex file of the generated tikz graph as follows:
\usepackage[graphics, active, tightpage]{preview}
% Created by tikzDevice version 0.10.1 on 2017-04-01 11:22:34
% !TEX encoding = UTF-8 Unicode
………. generated R tikz code here, per instance the R barplot from above………….
This will give you a tight beautiful look to the graph which is ideal for graphs that end up in academic papers or dissertations or theses.

Generating a PNG, JPEG…. images from your tikz graph via tools such as SIPS or ImageMagick

There is a great tool on MacOS called SIPS which is there by default.
On Linux there are some replications of SIPS and you can use the well known image manipulation tool imagemagick on both Linux and Windows
I will explain how to generate images  from tikz .tex on MacOS, please if you find yourself unable to replicate on Windows or Linux what I will explain later,  kindly write a comment and I am happy to add a mini tutorial on that.
The tikz .tex file should contain the commands I explained in the previous section meaning the commands that go in the preamble and at the end of the file.
Now go to the terminal on your MacOS and run pdflatex on the.tex file. This will generate a PDF file of the plot:
pdflatex -synctex=1 -interaction=nonstopmode BarplotHourSpent.tex
Then run the MacOS tool SIPS on the resulting PDF of your graph, you can specify what you want to generate from the PDF such as a JPEG
sips -s format jpeg BarplotHourSpent.pdf – -out BarplotHourSpent.jpg
or an PNG image
sips -s format png BarplotHourSpent.pdf – -out BarplotHourSpent.png
Hope you enjoyed this small tutorial!
Please check the other remaining articles in the series. Next is Part 2.

Leave a Reply

Your email address will not be published. Required fields are marked *