# User:Timothee Flutre/Notebook/Postdoc/2011/11/09

(Difference between revisions)
 Revision as of 19:49, 9 November 2011 (view source) (→Entry title: visualize NA in matrix)← Previous diff Current revision (12:24, 22 May 2014) (view source) (→Entry title: add custom heatmap) (One intermediate revision not shown.) Line 16: Line 16: rownames(mat) <- new.rownames rownames(mat) <- new.rownames - Or it's maybe easier with the built-in [http://stat.ethz.ch/R-manual/R-patched/library/base/html/replace.html replace] function. + Or it's maybe easier with the built-in [http://stat.ethz.ch/R-manual/R-patched/library/base/html/match.html match] function: + + new.rownames <- links\$id2[ match(rownames(mat), links\$id1) ] Line 25: Line 27: [[Image:Visualize_missing_values_in_matrix.png|400px]] [[Image:Visualize_missing_values_in_matrix.png|400px]] + + + * in R, customize the built-in heatmap (inspired from [http://stackoverflow.com/questions/5687891/r-how-do-i-display-clustered-matrix-heatmap-similar-color-patterns-are-grouped/5694349 this]): + + S <- 3  # nb of subgroups + V <- 7  # nb of observations + z <- matrix(c(0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,1,0,0), nrow=V, ncol=S, byrow=TRUE) + + myheatmap <- function(z, out.file="") { + def.par <- par(no.readonly=TRUE) + par(mar=c(4,5,3,2), font=2, font.axis=2, font.lab=2, cex=1.5, lwd=2) + if (out.file != "") + pdf(out.file) + layout(mat=cbind(1, 2), width=c(7,1))  # plot +  legend + mycol <- rev(heat.colors(4)) + image(x=1:NCOL(z), y=1:NROW(z), z=t(z), + xlim=0.5+c(0,NCOL(z)), ylim=0.5+c(0,NROW(z)), + xlab="", ylab="Observations sorted by cluster", main="Custom heatmap", + axes=FALSE, col=mycol) + axis(1, 1:NCOL(z), labels=paste("subgroup", 1:NCOL(z)), tick=0) + par(mar=c(0,0,0,0)) + plot.new() + legend("center", legend=sprintf("%.2f", seq(from=min(z), to=max(z), length.out=5)[-1]), + fill=mycol, border=mycol, bty="n") + if (out.file != "") + dev.off() + par(def.par) + } + + myheatmap(mydata.sort)

## Current revision

Project name Main project page
Previous entry      Next entry

## Entry title

• in R, replace the row names of a matrix by their new names when the order is different (but assuming one-to-one mapping):
```links <- data.frame(id1=c("a","b","c"), id2=c("1","2","3"), stringsAsFactors=FALSE)
mat <- matrix(runif(3*10), nrow=3)
rownames(mat) <- c("b","c","a")
rownames(mat) <- new.rownames
```

Or it's maybe easier with the built-in match function:

```new.rownames <- links\$id2[ match(rownames(mat), links\$id1) ]
```

• in R, visualize the missing values (encoded as "NA") in a matrix:
```image(1:nrow(mat), 1:ncol(mat), is.na(mat), col=c("white","black"),
main="Missing values", xlab="Genes", ylab="Samples")
```

• in R, customize the built-in heatmap (inspired from this):
```S <- 3  # nb of subgroups
V <- 7  # nb of observations
z <- matrix(c(0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,1,0,0), nrow=V, ncol=S, byrow=TRUE)

myheatmap <- function(z, out.file="") {
par(mar=c(4,5,3,2), font=2, font.axis=2, font.lab=2, cex=1.5, lwd=2)
if (out.file != "")
pdf(out.file)
layout(mat=cbind(1, 2), width=c(7,1))  # plot +  legend
mycol <- rev(heat.colors(4))
image(x=1:NCOL(z), y=1:NROW(z), z=t(z),
xlim=0.5+c(0,NCOL(z)), ylim=0.5+c(0,NROW(z)),
xlab="", ylab="Observations sorted by cluster", main="Custom heatmap",
axes=FALSE, col=mycol)
axis(1, 1:NCOL(z), labels=paste("subgroup", 1:NCOL(z)), tick=0)
par(mar=c(0,0,0,0))
plot.new()
legend("center", legend=sprintf("%.2f", seq(from=min(z), to=max(z), length.out=5)[-1]),
fill=mycol, border=mycol, bty="n")
if (out.file != "")
dev.off()
par(def.par)
}

myheatmap(mydata.sort)
```