Professional Documents
Culture Documents
(ye matey)
May 22,
Overview
A few things I want to try to cover
today:
Graphics
Basic plot types
Heatmaps
Working with plotting devices
Drawing plots to files
Graphics parameters
Drawing multiple plots per device
Writing functions in R
Parsing large files in R
Bar graphs:
barplot(
x=1:10,
names.arg=LETTERS[1:10],
col=gray(1:10/10)
);
Dot plots:
Alternative to boxplots when n is small
lo.vec <- rnorm(20,0,1);
hi.vec <- rnorm(20,5,1);
stripchart(
x=list(lo.vec, hi.vec),
group.names=c("low", "high"),
vertical=TRUE,
pch=19, method="jitter"
);
Heatmap basics
Supervised
Unsupervised
samples
genes
Scaling
By default, the heatmap()
function scales matrices by
row to a mean of zero and
standard deviation of one
(z-score normalization):
shows relative expression
genes
Clustering
Heatmaps are either:
ordered prior to plotting
(supervised clustering)
or clustered on-the-fly
(unsupervised clustering)
samples
Heatmap palettes
Some useful color palettes
bluered <- colorRampPalette(c("blue","white","red"))(256)
Heatmap3
Some of the problems with heatmap():
Cant draw multiple heatmaps on a single device
Cant suppress dendrograms
Requires trial-and-error to get labels to fit
Solution:
heatmap3(): a (mostly) backwards-compatible replacem
> x11()
> plot(1:10, 1:10)
> dev.copy2pdf(file="test2.pdf")
X11
2
> dev.copy(pdf,file="test3.pdf")
pdf
3
#
#
#
#
Graphics parameters
The par() function: get/set graphics
parameters
par(tag=value)
The ones Ive found most useful:
mar=c(bottom, left, top, right)
cex, cex.axis, cex.lab,
cex.main, cex.sub
xaxt=n, yaxt=n
bg
fg
las (0=parallel, 1=horizontal,
labels
2=perpendicular, 3=vertical)
lty
par(mfrow=c(2,3))
# then draw each plot
layout(matrix(data=1:6, nrow=2, ncol=3,
byrow=TRUE))
# then draw each plot
par(mfcol=c(2,3))
# then draw each plot
layout(matrix(data=1:6, nrow=2, ncol=3,
Save/load
my.matrix <- matrix(rnorm(100),10,10)
save(my.matrix, file="my.matrix.rdb")
rm(my.matrix)
load("my.matrix.rdb")
str(my.matrix)
num [1:10, 1:10] 2.582 -0.34 0.776 0.415 1.246 ...