[R] Sweave data-figure coupling

Sebastian P. Luque spluque at gmail.com
Mon Jan 5 20:52:00 CET 2009


Hi,

With the following Sweave minimal file:


---<--------------------cut here---------------start------------------->---
\documentclass{article}

\usepackage{Sweave}

\begin{document}

<<binom-sim>>=
thetas <- seq(0, 1, by=0.001)
prior <- rep(1, length(thetas)) / length(thetas)
lik <- dbinom(1, 1, thetas)
lik.p <- prior * lik
post <- lik.p / sum(lik.p)

<<binom-sim-fig, fig=TRUE, include=FALSE, height=3, echo=FALSE>>=
layout(matrix(1:2, ncol=2)); par(mar=c(5, 4, 2, 1), cex=0.75)
matplot(thetas, cbind(prior, lik, post), type="l", lty=c(2, 1, 1),
        xlab="theta", ylab="probability density")
lik <- dbinom(60, 100, thetas)
lik.p <- prior * lik
post <- lik.p / sum(lik.p)

matplot(thetas, cbind(prior, lik, post), type="l", lty=c(2, 1, 1),
        xlab="theta", ylab="probability density")
@

\includegraphics[width=\linewidth]{test-binom-sim-fig}


\end{document}
---<--------------------cut here---------------end--------------------->---

If the embedded chunks are evaluated directly in an R session, two
different panel data are produced by layout(), as expected.  However,
when weaving and latexing the file produces, then the two panels show
the same data, corresponding to the last data set.  This doesn't happen
if the data are much simpler, say using matplot() with simple sequences
of numbers.  Any ideas what is causing this and how to get around it?



Cheers,

-- 
Seb




More information about the R-help mailing list