[R] Sweave: infelicities with lattice graphics

Duncan Murdoch murdoch.duncan at gmail.com
Thu Jul 15 23:02:30 CEST 2010


On 15/07/2010 4:51 PM, Michael Friendly wrote:
> In a paper I'm writing using Sweave, I make use of lattice graphics, but 
> don't want to explicitly show (or explain)
> in the article text the print() wrapper I need in code chunks for the 
> graphs to appear. 
> I can solve this by including each chunk twice, with different options, 
> as in
> 
> <<ortho-xyplot1-code, keep.source=TRUE, eval=FALSE>>=
> library(nlme)
> library(lattice)
> xyplot(distance ~ age|Sex, data=Orthodont, type='b', groups=Subject, 
> pch=15:25,
>     col=palette(), cex=1.3, main="Orthodont data")
> @
> <<ortho-xyplot1,fig=TRUE, include=FALSE, echo=FALSE, width=6, height=6>>=
> library(nlme)
> library(lattice)
> print(xyplot(distance ~ age|Sex, data=Orthodont, type='b', 
> groups=Subject, pch=15:25,
>     col=palette(), cex=1.3, main="Orthodont data"))
> @
> 
> but then, if I decide to change the plotting code, I have to make 
> changes in two places.  Is there some
> feature of Sweave or lattice I've missed that would simplify this? 
> Re-use of code chunks doesn't seem
> to apply here.

If the lattice call is always last in the chunk, I believe this would 
work as a replacement for the second chunk:

<<ortho-xyplot1,fig=TRUE, include=FALSE, echo=FALSE, width=6, height=6>>=
<<ortho-xyplot1-code>>
print(.Last.value)
@

and then you'll only have to edit the first one.  But I haven't tried 
this, so something might go wrong...

Duncan Murdoch



More information about the R-help mailing list