Plots in Sweave

R is more than a calculator. It is a full graphics environment.

PlotsInSweave.rnw

%A Sweave template demonstrating plots
\documentclass[a4paper]{article}
\usepackage{amsmath,graphicx,textcomp}
\usepackage[utf8]{inputenc}

\title{Title of Document}
\author{Author Name}

\begin{document}
\maketitle
\section{Heatmap}
We show how to create a simple heatmap using generated random numbers
in R. First we generate some random numbers and some labels.

<<label=plot-data>>=
library(graphics)
N <- 10
M <- 5
mat <- matrix(rnorm(M*N), N)
rowLabels <- paste("r",1:N,sep="")
colLabels <- paste("c",1:M,sep="")
@

\noindent Then we plot.
<<label=fig1-plot>>=
pdf("example_heatmap.pdf",height=6,width=6)

hmap <- heatmap(mat, Rowv=NA, Colv=NA,main='A title',
                col = heat.colors(256), scale="column",margins=c(3,3),
                labCol=colLabels, labRow=rowLabels)
dev.off()
@

\begin{figure}
\begin{center}
\includegraphics[ext=.pdf,scale = 0.9]{"example_heatmap"}
\end{center}
\caption{This is the figure caption}
\end{figure}
\end{document}

Lets do a few things to this file

  1. Can you change the size of the matrix?
  2. Change some font sizes \footnotesize{}, \Large{}, \small{}
  3. Change some font properties \textbf{}, \textit{}
  4. Add \date{\today} to the preamble
  5. Can you remember how to make a table?

What about math?

P(A|B) = \frac{P(B|A)P(A)}{P(B)}

To make the above formula use \frac{}{}. Do yo recognize that formula?

Resources

Google knows all, but stackoverflow is almost as good.