Basic question on integration of ggplot2 & Sweave

210 views
Skip to first unread message

Ajay Shah

unread,
May 23, 2009, 9:32:41 AM5/23/09
to ggp...@googlegroups.com
I am a new user of ggplot2 and am trying to use it with Sweave. I
tried to write a minimal .Rnw file and it isn't behaving. When I step
through the lines of R code using emacs/ess, they work fine. But when
I run it through Sweave, I run into trouble. The Sweave stage works
fine but when I run pdflatex on the generated .tex file I get:

!pdfTeX error: pdflatex (file ./demo_sweave_ggplot2-p1.pdf): PDF inclusion: req
uired page does not exist <0>
==> Fatal error occurred, no output PDF file produced!

which seems to suggest that the generation of a pdf file containing
the figure didn't happen as expected.

The generated .tex file refers to a file-p1.pdf and this file exists
on the disk. But when I try to view it, it turns out to be
malformed. So pdflatex is right in complaining. It is the Sweave stage
that is broken; it is not producing a graph in a correct pdf file.

My minimal example file is:

---------------------------------------------------------------------------
\documentclass{article}
\begin{document}

\SweaveOpts{engine=R,eps=FALSE,results=hide,echo=FALSE}

<<start-off>>=
library(ggplot2)
r1=rnorm(100)
r2=rnorm(100, sd=1.5)
d <- data.frame(dates=seq(as.Date("2004-01-01"), by=1, length.out=101),
r1=c(0,r1), r2=c(0,r2),
p1=cumprod(c(100, (1+(r1/100)))),
p2=cumprod(c(100, (1+(r2/100)))))
@

\section{Simple scatter plot}

\begin{center}
<<p1,fig=TRUE,echo=TRUE>>=
ggplot(d, aes(dates, p1)) +
layer(geom="point")
@
\end{center}
\end{document}
---------------------------------------------------------------------------

I searched for "sweave" on the ggplot2 website and there was nothing.
I searched in the mailing list archives and there are a few people for
whom ggplot2 clearly seems to work with Sweave. Wonder what I'm doing
wrong!

--
Ajay Shah
ajay...@gmail.com
http://www.mayin.org/ajayshah
http://ajayshahblog.blogspot.com

Tobias Verbeke

unread,
May 23, 2009, 11:34:31 AM5/23/09
to Ajay Shah, ggp...@googlegroups.com
Hi Ajay,

Ajay Shah wrote:

> I am a new user of ggplot2 and am trying to use it with Sweave. I
> tried to write a minimal .Rnw file and it isn't behaving.

<snip>

> ---------------------------------------------------------------------------
> \documentclass{article}
> \begin{document}
>
> \SweaveOpts{engine=R,eps=FALSE,results=hide,echo=FALSE}
>
> <<start-off>>=
> library(ggplot2)
> r1=rnorm(100)
> r2=rnorm(100, sd=1.5)
> d <- data.frame(dates=seq(as.Date("2004-01-01"), by=1, length.out=101),
> r1=c(0,r1), r2=c(0,r2),
> p1=cumprod(c(100, (1+(r1/100)))),
> p2=cumprod(c(100, (1+(r2/100)))))
> @
>
> \section{Simple scatter plot}
>
> \begin{center}
> <<p1,fig=TRUE,echo=TRUE>>=
> ggplot(d, aes(dates, p1)) +
> layer(geom="point")
> @
> \end{center}
> \end{document}
> ---------------------------------------------------------------------------

You need to explicitly print the graph as in

print(ggplot(d, aes(dates, p1)) + layer(geom="point")

as when Sweaving auto-printing is not active
(as when you walk through the code submitting
interactively).

HTH,
Tobias

P.S. I notice you do not have

\usepackage{Sweave}

in your preamble.

Reply all
Reply to author
Forward
0 new messages