Tag Archives: r

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


Coloring every alternate table row

In one of my previous posts I describe how to color or shade every other row in a LaTeX table. However, there is a much nicer, cleaner and simpler way to color every odd/even row, making tables much more readable.

First load the xcolor package in the preamble:

\usepackage[table]{xcolor}

and define any color you like, e.g. light gray:

\definecolor{lightgray}{gray}{0.9}

Set the row color(s) just before opening the tabular environment for even and odd rows:

\rowcolors{<starting row index>}{<odd row color>}{<even row color>}

To use only one color, leave either of the row color arguments blank.

Complete code example:

\documentclass[11pt]{article}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\begin{document}
\begin{table}[ht]
\caption{default}
\begin{center}
\rowcolors{1}{}{lightgray}
\begin{tabular}{r|rrrrr}
  \hline
 & 1 & 2 & 3 & 4 & 5 \\
  \hline
1 & 2.36 & 1.08 & -0.49 & -0.82 & -0.65 \\
  2 & -0.68 & -1.13 & -0.42 & -0.72 & 1.51 \\
  3 & -1.00 & 0.02 & -0.54 & 0.31 & 1.28 \\
  4 & -0.99 & -0.54 & 0.97 & -1.12 & 0.59 \\
  5 & -2.35 & -0.29 & -0.53 & 0.30 & -0.30 \\
  6 & -0.10 & 0.06 & -0.85 & 0.10 & -0.60 \\
  7 & 1.28 & -0.46 & 1.33 & -0.66 & -1.80 \\
  8 & 0.80 & 0.46 & 1.37 & 1.73 & 1.93 \\
  9 & -0.75 & 0.28 & 0.51 & 0.19 & 0.58 \\
  10 & -1.64 & -0.12 & -1.17 & -0.10 & -0.04 \\
   \hline
\end{tabular}
\end{center}
\end{table}
\end{document}

Nope, the code box above was colored in a different way :-) .

And here is the result:

Table row shading example

Table row shading example

If you like the way Apple colors/colored tables, try using light blue:

\definecolor{lightblue}{rgb}{0.93,0.95,1.0}

I used R to generate the table:

library(xtable)
xtable(replicate(5, rnorm(10)))

Thanks to TJ Ellis for his comment.

 

Addendum:
Use the following code in your preamble to achieve the same effect automatically throughout the document. It redefines the tabular environment by adding the rowcolors command before \begin{tabular}.

\usepackage[table]{color}
\definecolor{lightgray}{gray}{0.9}
\let\oldtabular\tabular
\let\endoldtabular\endtabular
\renewenvironment{tabular}{\rowcolors{2}{white}{lightgray}\oldtabular}{\endoldtabular}


R: Convert table R2LaTeX

R and LaTeX work very well together. Let me show you how to generate a LaTeX table from a R variable.

First, install the xtable package if it’s not already.

install.packages("xtable")

Next load the package and create the tex-file with the table variable result. For illustration purposes, I create a matrix with random values.

library(xtable)
result <- replicate(6, rnorm(10)) # random matrix
print(xtable(result), type="latex", file="output.tex")

Since you probably don’t need an object that holds the LaTeX code, creating and writing the code to a file is combined in one command.

Note: You can control the number of digits using:

xtable(result, digits=-3)

The minus puts numbers into scientific format.


Follow

Get every new post delivered to your Inbox.

Join 316 other followers