Search code examples
rsweave

plot.MCA() not included in Sweave


I don't seem to be having a problem with any other method of including a plot via Sweave. However, plot.mca(), a method from the FactoMineR package seems to not have it's plot pulled through. It does create an Rplot.pdf file - but for whatever reason it's not renamed into "RnwFilename-00X.pdf" and not included in the resulting PDF when you compilePdf() it in RStudio.

Here's a trivial example, try it for yourself.

Note you may have to: install.packages("FactoMineR")

\documentclass[a4paper]{article}

% PREAMBLE
\begin{document}

\begin{center}
<<echo=false,fig=true>>=
library(FactoMineR)
x <- data.frame(
A=sample(letters[1:3],100,rep=T),
B=sample(letters[1:4],100,rep=T),
C=sample(letters[1:3],100,rep=T))
fit.mca <- MCA(x, graph=FALSE)
plot(fit.mca, invisible="ind")
@
\end{center}

\end{document}

Update - more details on the error message:

LaTeX errors:
!pdfTeX error: C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\pdflatex.EXE (file 
R:/.../RnwFilename-010.pdf): PDF inclusion: required page do
es not exist <0>

Solution

  • It works for me if I tell plot.MCA not to create a new device:

    plot(fit.mca, invisible="ind",new.plot = FALSE)
    

    Editorializing a bit, this seems like sub-optimal behavior for a plotting function, which most users (and other code, clearly) will expect to rely on R's default action to open a new device automatically. A plot function should only open a new device if the user has explicitly told it to (either by calling png, pdf etc or by actually setting new.plot = TRUE). Opinions may differ on this, though.