3

Is there a way to export plots from R directly to latex, more specifically TexStudio? I think exporting/saving the image from R and then importing it to R looses some of the quality. I saw on the internet that I can use tikz package and try to re-create the plot in Latex but that can't re-create all of my plots. Isn't there a way to do the code in R and export it directly to tex?

I'm trying to get the best image quality as possible for my thesis.

7
  • 1
    You should look into the sagetex package. This gives you access to the computer algebra system, SAGE, which includes R. The SAGE documentation mentioning R is here. A sample use of how SAGE is used with sagetex can be found by searching this site, for example, this post on statistics.
    – DJP
    Commented Oct 3, 2019 at 15:39
  • what format are you saving your plots, if they are a scalable format such as pdf you should not lose anything including them, if you save them as bitmap then... Commented Oct 3, 2019 at 15:41
  • @DavidCarlisle In pdf the quality of the images are amazing. Can I import pdf images to latex? Didnt know that!
    – Numbermind
    Commented Oct 3, 2019 at 15:43
  • yes of course \includegraphics{foo.pdf} (assuming you are using pdflatex or xelatex or lualatex) use eps if you are using classic latex. Commented Oct 3, 2019 at 15:44
  • @DavidCarlisle Just done that and it's exactly what I wanted! Can you answer the question so I can close this topic? thanks
    – Numbermind
    Commented Oct 3, 2019 at 15:47

5 Answers 5

5

If you save the plots in a scalable format such as PDF you should be able to include them with no loss of quaility in LaTeX using

\includegraphics{file.pdf}
4
\documentclass[twocolumn]{article}
\begin{document}

See my nice R plot in fig. \ref{fig:test}

<<test,dev='tikz',echo=F,fig.cap="Example plot">>=
with (iris, plot(Petal.Length,Petal.Width))
@

\end{document}

1) Save the above file with the .Rnw extension (e.g. test.Rnw).

2) Open it with RStudio.

3) Push the "Compile PDF" button.

4) Take care to know about the R package knitr

5) Enjoy it.

4

tikzDevice

The accepted answer is one easy way to get scalable graphics from R to LaTeX. It does not, IMHO, answer the actual question, since it does not export the figure "directly" (which I interpret to mean "LaTeX-readable code").

Disclaimer: My answer is mostly just a more beginner friendly version of this answer.

To achieve our objective, we can use the R library tikzDevice.
To install it, just use install.packages("tikzDevice") inside RStudio.

In your R script

library(tikzDevice)
tikz('figs/simpleEx.tex',width=3.5,height=3.5)
plot(1,main='Hello World!')
dev.off()

This will save the output of install.packages("tikzDevice") to the subdirectory figs (you have to create the subdirectory in advance) under the name simpleEx.tex.
Don't forget dev.off(), since it closes the device, which is what you want to do.

In your LaTeX document

\documentclass{article}
% All LaTeX documents including
% tikz() output must use this
% package!
\usepackage{tikz}

\begin{document}
\begin{figure}[!h]
\centering
% The output from tikz()
% is imported here.
\input{simpleEx.tex}
\caption{Simple Example}
\end{figure}

\end{document} 

A quick word about paths

Don't forget that you can set a relative or absolute path for \input{}, so if you have saved your figures to /projectdirectoy/rdirectory/figs/simpleEx.tex, but your LaTeX file is in /projectdirectory/latexdirectory/latex.tex, you can use \input{../rdirectory/figs/simpleEx.tex}. But of course you can just save the figures directly in your latex directory in the first place, if you wish to do so, by modifiying the tikz('figs/simpleEx.tex',width=3.5,height=3.5) line to tikz('../latexdirectory/figs/simpleEx.tex',width=3.5,height=3.5), for example.

More examples

I have stolen the code examples from the vignette for tikzDevice, where you can find more examples, some of them more elaborate.

3

The standard way of doing this is, or used to be, TikzDevice.

I used it many years ago, and it worked well then. The original author retired from the fray some years ago, but it looks like it is still maintained, more or less.

You're probably better off asking for help directly from the developer(s), though. Or possibly from an R forum. This is really more of an R question, rather than a TeX question.

0

You need to look at knitR, which allows you put your R code into what is called chunks inside your LaTeX document with full control what is to show up in the final PDF. A bit of a learning curve but very powerful, because whenever go generate the PDF R will generate the plots (or tables) for you.

el

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .