[R] Sweave: side by side dynamic graphs

Dieter Menne dieter.menne at menne-biomed.de
Mon Nov 3 08:40:40 CET 2008

Felipe Carrillo <mazatlanmexico <at> yahoo.com> writes:

> I'm trying to create some side by side dynamic graphics on the same page but

Thanks for the example; there was a minor typo so that it did not run "plot(}",
and a major problem that could have caused serious problem (happens to me quite
often due to copy and paste)

<<fig.R,echo=F,fig=T,width=2.5,height=2.5>>=

You must give the << sections with graphics different names, otherwise the wrong
picture will be used.

For side-by-side, you simply plot side by side, or use minipages when captions
are needed for both figures. In the first case, trellis graphs are much more
space-conservative.

Dieter

\documentclass[12pt]{article}
% For a minimal example, please remove all special packages
\begin{document}
\begin{figure}[h]
\centerline{\mbox{\includegraphics[width=3.00in]{bar}}
\mbox{\includegraphics[width=3.00in]{bar}}}
\end{figure}

\begin{figure}[ht]
\centering
<<Rdouble,echo=F,fig=T,width=5,height=2.5>>=
par(mfrow=c(1,2))
plot(1:10)
plot(1:10)
@
\caption{When you do not need separate captions, this is the easiest way.}
\end{figure}

\begin{figure}[ht]
\begin{minipage}[b]{0.5\linewidth}
\centering
<<RsingleA,echo=F,fig=T,width=2.5,height=2.5>>=
plot(1:10)
@
\caption{First figure}
\label{fig:figure1}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.5\linewidth}
\centering
<<RsingleB,echo=F,fig=T,width=3,height=3>>=
plot(1:15)
@
\caption{Second figure}
\label{fig:figure2}
\end{minipage}
\end{figure}
\end{document}