[R] using Sweave, how to save a plot in a given size

cls59 sharpsteen at mac.com
Wed Apr 15 19:41:37 CEST 2009



Lo_Lo wrote:
> 
> 
> I just want to add that I didn't have any overful box before I use :
> 
> <<label=fig1, fig=FALSE, results = tex>>=
> pdf("fig1.pdf", 
> width = wid, heigth = hei)
> plot(1:10)
> plot(1:10)
> dev.off()
> cat("\\begin{figure}[h]")
> cat("\\centering")
> cat("\\multido{\i=1+1}{7}{\includegraphics[page=\i,width=1.5in, height =
> 1.5in]{images.pdf}}") 
> cat("\\end{figure}")
> @
> 

Here's a couple of pointers for using figures in Sweave that might help you
out.

1. Only one plot per figure chunk- Sweave gets confused if you use more than
one *high level* plotting command.

2. Don't call the pdf() function- the Sweave driver does this automatically
if you set pdf=T in the \SweaveOpts or the << >> code chunk header.

3. Don't use cat() to output the \begin{figure}, \end{figure},
\includegraphics, ect. These will end up in the wrong place.

>From the look of your example, this is what you might want to include in
your .Rnw file:

\begin{figure}[h]
\centering

% Replace width and height with whatever dimensions you need. Leave them as
bare numbers
% i.e. 5 not 5in.
<<fig1,echo=F,fig=T,pdf=T,width=5,height=5>>=
plot(1:10)
@
% You can put a // here to cause the figures to separate onto two lines.
<<fig2,echo=F,fig=T,pdf=T,width=5,height=3>>=
plot(1:10)
@
% It is very important that all code chunks have different names! I.e.
<<fig1>>, <<fig2>> ect.
\end{figure}

The result of running Sweave on filename.Rnw will be:

\begin{figure}[h]
\centering
\includegraphics{filename-fig1}
\includegraphics{filename-fig2}
\end{figure}

Hope this helps!

-Charlie

-----
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://www.nabble.com/using-Sweave%2C-how-to-save-a-plot-in-a-given-size-tp23036715p23063594.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list