# Using RStudio and LaTeX

This post will explain how to integrate RStudio and LaTeX, especially the inclusion of well-formatted tables and nice-looking graphs and figures produced in RStudio and imported to LaTeX. To follow along you will need RStudio, MS Excel and LaTeX.

## Using tikzdevice to insert R Graphs into LaTeX

I am a very visual thinker. If I want to understand a concept I usually and subconsciously try to visualise it. Therefore, more my PhD I tried to transport a lot of empirical insights by means of visualization. These range from histograms, or violin plots to show distributions, over bargraphs including error bars to compare means, to interaction- or conditional effects of regression models.

For quite a while it was very tedious to include such graphs in LaTeX documents. I tried several ways, like saving them as pdf and then including them in LaTeX as pdf, or any other file format. Until I found tikzDevice. Tikz is an R Package that translates (almost) any R graph in a LaTeX file which you can easily include in your file. You only have to indicate the file name, as well as the width and height of the graph to get beautiful and publication ready graphs for your LaTeX mannuscript.

Read on to see how it works.

library(tikzDevice) # load the tikzDevice package
library(ggplot2) # load the ggplot2 package

# Create the plot
p <- ggplot(data = mtcars, aes(x = cyl, y = mpg)) +
geom_bar(stat = "identity")

# Create the tex file with tikzDevice
tikz(file = 'p_tex.tex', width = 4.5, height = 4.5)
p
dev.off()

Copy this file to the folder in which the LaTeX document you want to include this graph in is. Include the graph with the following lines of code in LaTeX.

\begin{document}
\begin{figure}[h]
\begin{tabular}{@{}c@{\hspace{.5cm}}c@{}}
\includegraphics[page=1,width=1\textwidth]{p_tex}
\end{tabular}
\end{figure}
\end{document}

## Using excel2latex and sjPlot to insert R-tables into LaTeX

Here, you first need to install an Excel Plugin (Add-in) called Excel2Latex. Download it here. Just open it anywhere and it should appear under the Add-ins header in Excel. The following example shows how to create a table for regression results with sjPlot.

library(sjPlot) # load the sjPlot package

# Create a simple linear model
lm <- lm(mpg ~ cyl, mtcars)

# Create an output table of the linear model with sjstats package
sjt.lm(lm)

Open it in your internet browser, copy it to excel, edit it if you want, and then use excl2latex in order to get the latex code (table or tabular environment).

## 4 thoughts on “Using RStudio and LaTeX”

1. Aníbal Ayala says:

Hola, I think Rmarkdown can do the trick quite well, without the need of export to latex.

Regards,

Aníbal.

Liked by 1 person

1. Hey Anibal,
true, RMarkdown can be awesome to create parameterized reports. However, based on my experience that can take some time. Problem is that most of my colleagues use Latex and don’t want to switch to RMarkdown.

Cheers,
Hendrik

Like

2. Eric says:

Umm … have you heard of knitr and xtable? I’ve been doing this for decades (with Sweave before knitr and S+ before R) with a lot less trouble. There’s no need for manual formatting in EXcel.

Like

1. Hey Eric,
I tried out xtable a while ago and if I remember correctly I went the detour via Excel because there it was a bit easier to make small changes, like changing variable names, formatting lines, etc., which I felt was not so easy with xtable.

Like