Search code examples
rlatexsweave

side-by-side figures in Sweave


I use this code to make figures in Sweave

<<label=fig1plot, include=FALSE >>=
plot(cars)
@

\begin{figure}
\begin{center}
<<label=fig1, fig=TRUE, echo=FALSE>>=
<<fig1plot>>
@
\end{center}
\caption{Some caption}
\label{fig:fig1plot}
\end{figure}


<<label=fig2plot, include=FALSE >>=
plot(table(rpois(100,5)), type = "h", col = "red", lwd=10, main="rpois(100,lambda=5)")
@

\begin{figure}
\begin{center}
<<label=fig2, fig=TRUE, echo=FALSE>>=
<<fig2plot>>
@
\end{center}
\caption{Some caption}
\label{fig:fig2plot}
\end{figure}

Now I want to put these two figures side-by-side and have captions like Fig 1 (a) and Fig 1 (b). Any ideas, comments and guidelines are highly appreciated. Thanks in advance for your time and help.


Solution

  • You can use the subcaption LaTeX package:

    \begin{figure}
      \begin{minipage}[b]{.5\linewidth}
         \centering\large A
         % plot 1
         \subcaption{A subfigure}\label{fig:1a}
      \end{minipage}%
      \begin{minipage}[b]{.5\linewidth}
         \centering\large B
         % plot 2
         \subcaption{Another subfigure}\label{fig:1b}
      \end{minipage}
      \caption{A figure}\label{fig:1}
    \end{figure}
    

    See also this: How can I have images that are side-by-side and have numbers attached to each one?