Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include complicated R plots in a LaTeX document?

Tags:

r

pdf

latex

sweave

I'm having the problem with the following code snippet:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics{myscatterplot.pdf}
\end{document}

Where "myscatterplot.pdf" is generated by the following code in R:

library(scatterplot3d)
pdf("myscatterplot.pdf")
scatterplot3d(rnorm(100), rnorm(100), 1:100, highlight.3d = TRUE)
dev.off()

The problem is that the resulting LaTeX document when compiling with texworks (pdfLatex+makeindex+bibtex) has the graph axes, but none of the points in the plot or axis labels(In this case, it is just the 3d axes themselves). There are no error or warning messages output by R or LaTeX. I am using:

  • R 2.12.1 on Windows 7,
  • MiKTeX 2.8,
  • TeXworks
  • Adobe Reader 9 (not sure if this is relevant...)

I have been able to use the \includegraphics command to include a png version of the figure, and opening "myscatterplot.pdf" with adobe shows the figure I want in my document.

I have tried to use the tikz package as a workaround, but it seems there is so much information generated by scatterplot3d that the resulting figure cannot be included in the latex document due to memory size (error (my actual plot will have 10000 + points!).

I have a suspicion that the problem is due to the fonts in the ".pdf" file, but I have tried to change the pdf fonts using

pdf("changefont.pdf")
par(family = "Helvetica")
scatterplot3d(rnorm(100), rnorm(100), 1:100, highlight.3d = TRUE)
dev.off()

with precisely the same result when using \includegraphics(changefont.pdf).

The other possible problem I am considering is that maybe the scatterplot3d output is actually multiple images, and \includegraphics is only taking the first of the figures (the axes) from the pdf file. In this case, I'm still not sure how to work around it.

I would really appreciate a workaround, as I would eventually like to do all of this with Sweave and this is making me bitter toward the otherwise beautiful output of the package!

Thank you in advance for your responses.

Edit 1:

So, the first recommendation was to use the EPS format instead of pdf. This yielded some results, but not what I wanted. I ran the following:

\documentclass{article}
\usepackage{graphicx,epstopdf}
\begin{document}
\begin{figure}
\includegraphics[angle = 270, width= 6in, keepaspectratio=true]{change.eps}
\end{figure}
\end{document}

I generated "change.eps" using

postscript("change.eps")
scatterplot3d(rnorm(100), rnorm(100), 1:100, highlight.3d = TRUE)
dev.off()

This did yield an improvement (despite the fact that it, strangely enough, rotated the plot 90 degrees clockwise in the latex output!), and I now have the axes and the points from the scatterplot in my latex output! However, the axis labels are still not on the figure, even though I have opened "change.eps" using ghostview, and the axes are in the plot! It seems the way scatterplot3d outputs figures doesn't agree with the way \includegraphics searches for figures...

So, I'm still looking for a solution to this that will include axes labels.

like image 860
Rguy Avatar asked Jan 24 '11 03:01

Rguy


2 Answers

Looking at myscatterplot.pdf as generated with the commands you listed, the axes and labels are there. However, the pdf is rather large (7in x 7in).

Does it help if you play with the weight/height parameters to pdf()?

pdf("myscatterplot.pdf", height=3.5, width=3.5)
like image 161
Karsten W. Avatar answered Oct 13 '22 05:10

Karsten W.


This is a side note related to LaTeX exceeding its available memory.

I just used your example with the tikzDevice to do some stress tests and it looks like the results are pretty dependent on which TeX engine is used. Of particular note is luatex, the sucessor to pdftex, which proved capable of handling plots with many graphical elements.

  • pdflatex:

    Made it through a plot with 1000 points, exceeded memory and died at 10,000.

  • xelatex:

    Also exceeded memory and died at 10,000 points.

  • lualatex:

    Chewed through 10,000 points in ~45 seconds and produced a 1 MB PDF file. Slogged through 100,00 points (a 10 MB input file) in ~7.5 minutes, spat out a 8.5 MB PDF file and peaked at ~750 MB of memory usage. Didn't try the 1 million benchmark.

It looks like pdftex and xetex allocate all of their memory up front when the program lanches and that is all they will ever get. luatex on the other hand seems like it can dynamically allocate memory and would therefore only be constrained by the amount of RAM available.

So, if pdflatex is giving you "out of memory" errors, try taking lualatex for a spin!


These tests were done using TeX compilers included in the TeX Live 2010 distribution. I am also one of the authors of the tikzDevice

like image 43
Sharpie Avatar answered Oct 13 '22 06:10

Sharpie