# R: Controlling size and placement (subfig) of Sweave-generated figures

I’ve been writing quite a bit of R code recently for my gene expression analysis. Also, I use Sweave (manual) which allows embedding R code “chunks” into TeX documents. When the code is being evaluated, the output (code, calculation results, plots, tables, etc.) will automatically integrated into the document as TeX syntax, making it an extremely handy tool for presentation and documentation purposes.
That said, I will now show how to get a better control of the size and placement of figures generated by Sweave code chunks by manually inserting them into a TeX document.

Let me first give a general Sweave example with a single plot and later extend it with a second, placing them side-by-side. The following code chunk defines a vector x and a function f.

<<echo=F, results=hide>>=
x <- seq(-2, 2, by=0.1)
f <- function(x) { x^3 }
@

Now we use Sweave to plot the function f and integrate the result into the TeX document:

\begin{figure}
\begin{center}
<<fig=TRUE, echo=FALSE>>=
plot(x, f(x), cex=0.3)
@
\caption{Function plot of $x^3$ between $-2$ and $2$.}
\end{center}
\end{figure}

And here is the result:

Imagine now that we want to place plots of two functions, $f(x)=x^2$ and $f(x)=x^3$, side-by-side using the subfig package. This cannot be done automatically, since there is no way to control the figure size in the final pdf directly from within the Sweave chunk. Therefore, we generate the figures and add them manually, giving us complete control of size, angle and trimming of the figure (see graphicx).

Here is the complete code:

\documentclass[11pt]{article}
\usepackage{graphicx, subfig}
\begin{document}

<<echo=FALSE, results=hide>>=
x <- seq(-2, 2, by=0.1)
f2 <- function(x) { x^2 }
f3 <- function(x) { x^3 }
@

\begin{figure}[ht]
\begin{center}
\subfloat[Function plot of $x^2$ between $-2$ and $2$.]{

<<label=xsquare, fig=TRUE, echo=FALSE, include=FALSE>>=
plot(x, f2(x), cex=0.3)
@

\includegraphics[width=0.4\textwidth]{sweave-xsquare}
}
\subfloat[Function plot of $x^3$ between $-2$ and $2$.]{

<<label=xcube, fig=TRUE, echo=FALSE, include=FALSE>>=
plot(x, f3(x), cex=0.3)
@

\includegraphics[width=0.4\textwidth]{sweave-xcube}}
\end{center}
\end{figure}
\end{document}

Basically, the code generates the plots (pdfs). We don’t want them to be automatically included into the document, which is achieved through the include=FALSE option. Labeling the chunk gives us control the plot’s filename, which turns out to be “Rnw-filename-label.pdf” (Rnw is a Sweave file extension). In case you prefer the figures not to have the filename of your source-file, there is a Sweave option that globally controls the figure-filename-prefix and the directory to which they are saved:

\SweaveOpts{prefix.string=<directory>/<filename-prefix>}

Finally, we manually add our plots using the standard LaTeX \includegraphics-command and the subfig-package (see this post for more details). In case you prefer png and/or eps figures, use the pdf, png, eps options:

<<label=xcube, fig=TRUE, echo=FALSE, pdf=FALSE, eps=TRUE, png=TRUE, include=FALSE>>=