12
votes

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.

6
I know this will not answer your question, but If I were you, I would not include a plot with 10.000+ points in vectorized format, as the resulting pdf would be really huge indeed. Save your plot in png with appropriate resolution (as you did) and include that image in your document.daroczig
Just that you know: Your original example code works flawlessly with Ubuntu 10.10 (R 2.12.1 Texlive 2009). You could try updating to Miktex 2.9 and see if it helps.Matti Pastell

6 Answers

5
votes

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)
4
votes

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

3
votes

You can try Sweave: http://www.statistik.lmu.de/~leisch/Sweave/ (Sweave is a tool that allows to embed the R code for complete data analyses in latex documents)

1
votes

If you want to insert pdf images into your file, I think you have to use pdflatex instead of standard latex. If that isn't installed, I'd try generating the figures in eps format from R, instead of pdf.

1
votes

I recall struggling with similar problem. Don't remember if I could see labels in the figure though. Looking backwards at the code there are two parameters which I modified at R level:

par(xpd=NA)
par(oma=c(3, 3, 0, 0))

also for postscript in order to not have graphs rotated by 90 degrees:

postscript(file="xx.eps", horizontal=FALSE)

HTH

1
votes

I actually came here with the exact same problem (TexWorks, pdfLatex, Windows 7). And I found something interesting: when I first start Latex, the figures appeared with axes and all, and when I re-ran the pdfLatex, the axes disappeared again. This gave me a hunch, and I figured it out: even though the latex preview does not show the axes, the PDF format does. And you don't even have to tell the \includegraphics command that the graphics is in PDF format.

pdf("C:/Users/Orr/Documents/Leiden University/Master thesis/Chapters/Images/bioRes/Boxplots mouse raw data.pdf")
boxplot(mouse_data_raw,main="Mouse raw data")
dev.off()

\begin{figure}[t]
\includegraphics[scale=0.5]{mouse-box-raw}
\includegraphics[scale=0.5]{human-box-raw}
\end{figure}