Skip to content
Prev 177182 / 398503 Next

using Sweave, how to save a plot in a given size

Lo_Lo wrote:
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.
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