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

Rguy picture Rguy · Jan 24, 2011 · Viewed 8.2k times · Source

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.

Answer

Karsten W. picture Karsten W. · Jan 24, 2011

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)