About these ads

Tag Archives: includegraphics

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:
Sweave1

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}
}
\qquad
\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>>=

Sweave2


Placing figures/tables side-by-side (\subfig)

The subfigure package was replace by the subfig package quite a while ago. I therefore decided to replace my old post on that topic (Placing figures/tables side-by-side with subfigure) with an introduction to the subfig package. The package simplifies the positioning, captioning (I wonder if that’s a word) and labeling of small “sub” figures and tables within a single figure or table environment.

So much for the theory, let’s have a look at the usage. First we load the package, the options tell LaTeX to include the subs in the listoffigures and listoftables. Most probably you will also need the graphicx package, e.g. to set the figure width.

\usepackage[lofdepth,lotdepth]{subfig}
%\usepackage{graphicx}

The subfloat command has a body, for the content, being most commonly a figure/table plus a label. The two optional arguments define the list-of-figures text and the caption. If only one is provided, the text will be used for both, somewhat similar to the caption command (\caption[⟨list entry⟩]{⟨caption⟩}).

\subfloat[⟨list entry⟩][⟨sub-caption⟩]{⟨body⟩}

We need the figure (or table) floating environment as a container for the actual subfigure/subtables. This leads us to a complete subfigure example with two subfloats:

\begin{figure}[h]
\centering
 \subfloat[][]{
   \rule{4cm}{3cm}
 }
 \subfloat[][]{
   \rule{4cm}{3cm}
 }
\end{figure}

Referencing a subfloat is straight forward. The first code line will produce the complete reference number, e.g. 4a, whereas the second line will only produce the sub-index, e.g. (a).

\ref{⟨label⟩}
\subref{⟨label⟩}

By default, the package will place the figures/tables side-by-side. Now in case you want to arrange 4 subfloats 2×2, you make use of the standard linebreak by leaving an empty line.

The following is a complete code example. You will find a link to the expected result further below.

\documentclass[11pt, a4paper, draft]{article}
\usepackage{graphicx}
\usepackage[lofdepth,lotdepth]{subfig}
\begin{document}
\listoftables
\listoffigures
\section{Example with 3 "sub" tables}
\begin{table}[ht]
\centering
\subfloat[Subtable 1 list of tables text][Subtable 1 caption]{
\begin{tabular}{l|ccc}
& 1 & 2 & 3\\
\hline
1 & A & B & C\\
2 & D & E & F\\
\end{tabular}}
\qquad
\subfloat[Subtable 2 list of tables text][Subtable 2 caption]{
\begin{tabular}{l|ccc}
& 1 & 2 & 3\\
\hline
1 & A & B & C\\
2 & D & E & F\\
\end{tabular}}
\qquad
\subfloat[Subtable 3 list of tables text][Subtable 3 caption]{
\begin{tabular}{l|ccc}
& 1 & 2 & 3\\
\hline
1 & A & B & C\\
2 & D & E & F\\
\end{tabular}}
\caption{This is a table containing several subtables.}
\end{table}
\clearpage
\section{Example with 4 "sub" figures 2x2}
\begin{figure}[h]
\centering
\subfloat[Subfigure 1 list of figures text][Subfigure 1 caption]{
\includegraphics[width=0.4\textwidth]{figure1.jpg}
\label{fig:subfig1}}
\qquad
\subfloat[Subfigure 2 list of figures text][Subfigure 2 caption]{
\includegraphics[width=0.4\textwidth]{figure2.jpg}
\label{fig:subfig2}}
\subfloat[Subfigure 3 list of figures text][Subfigure 3 caption]{
\includegraphics[width=0.4\textwidth]{figure3.jpg}
\label{fig:subfig3}}
\qquad
\subfloat[Subfigure 4 list of figures text][Subfigure 4 caption]{
\includegraphics[width=0.4\textwidth]{figure4.jpg}
\label{fig:subfig4}}
\caption{This is a figure containing several subfigures.}
\label{fig:globfig}
\end{figure}
In the text, you can refer to subfigures of figure \ref{fig:globfig} as \ref{fig:subfig1}, \ref{fig:subfig2}, \ref{fig:subfig3} and \ref{fig:subfig4} and to the sub-index as \subref{fig:subfig1}, \subref{fig:subfig2}, \subref{fig:subfig3} and \subref{fig:subfig4}.
\end{document}

The PDF result can be viewed here.

The complete documentation of this very comprehensive package is available here.

A final remark:
In order to simulate figures for testing purposes, you may either use the draft option of the documentclass command which lets you load existing or non-existing figure files and draws an empty box as placeholder. Or you may make use of the rule command that by default draws a black line of a certain length and thickness.

\documentclass[draft]{article}
...
\includegraphics[width=0.3\textwidth]{virtualfigure.jpg}
\rule{4cm}{3cm}

Beamer: An introduction to LaTeX presentations

Beamer is a LaTeX document class that provides extensive functionality to create presentations. Here, I will only show the basics and after reading this guide you will be able to create a simple presentation in LaTeX. I am aware there are a lot of tutorials available out there and this is not different from any other tutorial. I hope however, I can encourage some of you who have hesitated so far, for whatever reason, to create your next presentation with LaTeX. And I’m sure it will be a lot of fun, with similar effort. I should add that the output will obviously be a PDF file (with all its advantages!). Luckily, PDF-viewers (including Adobe Acrobat) provide a fullscreen-mode for presentation purposes.

So lets get started!

\documentclass{beamer}

Now that line is straight forward, not much to say about it. Once that’s done, we have to choose a theme. This website gives a visual overview of the most common themes. I like Singapore:

\usetheme{Singapore}

Next, still in the preamble, we prepare the title page, using a similar set of commands as for other document-classes:

\title{Your Presentation Title}
\author{The author}
\date{February 4, 2011}

A frame may have one or several slides. Since PDFs are static, dynamic “effects” such as adding more content to a frame are achieved by two consecutive slides in the output file.

We use the previously defined title page to create our first (single-slided) frame:

\begin{frame}
\titlepage
\end{frame}

Similar to articles, sections, subsections, etc. are available and can be used to define an outline, printed with \tableofcontents. For many themes, the outline will be displayed in the header/footer and provides direct access to a certain section of the presentation. Frame-titles are created using \frametitle{Title}.

Most of the time, a frame will show a list of items created through the well known itemize-environment:

\begin{frame}
\frametitle{Title of the Frame}
\begin{itemize}
\item First item
\item Second item
\item ...
\end{itemize}
\end{frame}

Now what if you don’t want to show all the items at once, but one after another. The \pause-command will take care of it. Just add it anywhere you want to “pause” and will produce 3 slides. In presentation mode, the next bit of information is only shown after you press a key (usually space or arrow keys). So the above code example now looks as follows:

\begin{frame}
\frametitle{Title of the Frame}
\begin{itemize}
\item First item \pause
\item Second item \pause
\item ...
\end{itemize}
\end{frame}

Figures are used similarly as within other document-classes:

\usepackage{graphicx}
...
\begin{figure}
\includegraphics[scale=0.5]{img.jpg}
\caption{Sample caption.}
\end{figure}

Finally, I will show you something a little more advanced. Two columns, with items on the left and figures on the right side. The idea is to show an item along with an image. We want one item after the other to appear, while the image replaces the previous. Let me give you the code first and then explain some of the details:

\begin{frame}{A More Advanced Example}
\begin{columns}
\begin{column}{5cm}
\begin{itemize}
\item<1-> Figure 1
\item<2-> Figure 2
\item<3-> Figure 3
\end{itemize}
\vspace{2cm}
\end{column}
\begin{column}{5cm}
\includegraphics<1>[scale=0.1]{img1.jpg}
\includegraphics<2>[scale=0.1]{img2.jpg}
\includegraphics<3>[scale=0.1]{img3.jpg}
\end{column}
\end{columns}
\end{frame}

What’s new here is called overlay specification within an environment (itemize) and lets you display different text/content on different slides or a range of slides.

<1-> indicates that this item will be displayed from slide 1 onwards in this frame. We could also have used <1-3>. Whereas the actual figures will only be displayed on their specific slide, e.g. <1>.

If there is no environment, the set of things to display has to be enclosed by the overprint-environment:  \begin{overprint}...\end{overprint}.

Another thing is the “overlay specification” for commands, e.g. to change the text-color for slides 2 and 3:

\color<2-3>[rgb]{1,0,0} This text is red on slides 2 and 3, otherwise black.

Here, the overlay specification always has to follow the command before any additional arguments. I have to admit, slightly useless, but at least it illustrates the result nicely (example was taken from the user guide page 81).

The following a complete code sample with a few frames containing the various examples described above:

\documentclass{beamer}
\usepackage{graphicx}
\usetheme{Singapore}
\title{Presentation Title}
\author{The Author}
\date{May 4, 2011}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}
\section{List of Items}
\begin{frame}
\frametitle{List of Items}
\begin{itemize}
\item First item \pause
\item Second item \pause
\item ...
\end{itemize}
\end{frame}
\section{Figure Example}
\begin{frame}
\frametitle{Figure Example}
\begin{figure}
\includegraphics[scale=0.1]{img1.jpg}
\caption{Sample caption.}
\end{figure}
\end{frame}
\section{Overlay Specification}
\begin{frame}
\frametitle{Overlay Specification}
\begin{columns}
\begin{column}{5cm}
\begin{itemize}
\item<1-> Figure 1
\item<2-> Figure 2
\item<3-> Figure 3
\end{itemize}
\vspace{3cm}
\end{column}
\begin{column}{5cm}
\includegraphics<1>[scale=0.1]{img1.jpg}
\includegraphics<2>[scale=0.1]{img2.jpg}
\includegraphics<3>[scale=0.1]{img3.jpg}
\end{column}
\end{columns}
\end{frame}
\end{document}

The packages hyperref, xcolor, color are automatically loaded when using the beamer class.

A comprehensive user guide can be downloaded from CTAN.


Follow

Get every new post delivered to your Inbox.

Join 713 other followers

%d bloggers like this: