How to arrange column in heatmap.2() based on a predefined order

plot, r

Solution

I do agree the help page wasn't entirely clear, but after a bit of experimentation I discovered that you could prevent column ordering with FALSE and order the columns at the time of input. After seeing this to be the case, the help page was not wrong in any respect.

heatmap.2(as.matrix(mydata[,cn]), Colv=FALSE, 
          dendrogram="row",trace="none", margin=c(8,9), 
          hclust=hclustfunc,distfun=distfunc)

Problem

The following code ``` library("gplots") mydata <- mtcars hclustfunc <- function(x) hclust(x, method="complete") distfunc <- function(x) dist(x,method="euclidean") heatmap.2(as.matrix(mydata),dendrogram="row",trace="none", margin=c(8,9), hclust=hclustfunc,distfun=distfunc); ``` Generate a heat map that looks like this: Note that in that figure the column is ordered automatically by the function `cyl am vs carb wt drat gear gseq mpg hp dsp` What I want to do is to create the same heatmap but with my personally defined column order: ``` cn <- c("wt","qsec","vs","am","gear","carb", "mpg","cyl","disp","hp","drat" ) ``` How can I achieve that? I tried using `Colv` like this but failed: ``` heatmap.2(as.matrix(mydata),Colv=cn,dendrogram="row",trace="none", margin=c(8,9), hclust=hclustfunc,distfun=distfunc); ```

Original source