[R] Sweave and multiple figures from an R source file

Duncan Murdoch murdoch at stats.uwo.ca
Mon Apr 12 18:40:18 CEST 2010


On 12/04/2010 12:08 PM, michele donato wrote:
> Hi,
> I am trying to get figures from multiple source files in Sweave. My test
> file is as follows
>
> \documentclass{article}
> \usepackage{Sweave}
> \begin{document}
> \begin{section}{notitle}
>   This is a simple Sweave test
> <<gethypergraphs_rhea,fig=TRUE,echo=F>>=
> source("./testfig.r")
> @
> \\
> End of the simple sweave test
> \end{section}
> \end{document}
>
> where testfig.r is as simple as
>
> plot(1:20)
> plot(1:5)
>
> The figures are correctly generated in the folder, as a .pdf/.eps with
> two pages, each page a plot
>
> the resulting tex file is
>
> \documentclass{article}
>
> \usepackage{Sweave}
>
> \begin{document}
> \begin{section}{notitle}
>   This is a simple Sweave test
> \includegraphics{coupling_images_report-gethypergraphs_rhea}
> \\
> End of the simple sweave test
> \end{section}
> \end{document}
>
> When I compile the .tex file, however, the figures are overlapped one on
> top of the other.
> I tried to search for a solution, but found none...

This doesn't really have to do with source() or even Sweave:  
\includegraphics wants just one figure per file.  In Sweave, that means 
you need to put separate figures in separate code chunks (or use 
par(mfrow= ...) or something to allow both to be plotted on the same page).

Duncan Murdoch



More information about the R-help mailing list