Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

plot

r

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:

enter image description here

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);
like image 345
pdubois Avatar asked Dec 10 '13 02:12

pdubois


People also ask

How do I order rows in heatmap R?

heatmap for a predetermined order of rows. Simply reorder the rows of your second matrix to match the first one, then use for example R's heatmap() function setting Rowv=FALSE to prevent it from automatically reordering the rows, alternatively, you could set Rowv to a vector defining the order.

What package is heatmap 2 in R?

The heatmap. 2 function from the gplots package allows to produce highly customizable heatmaps. Useful arguments include: Rowv, Colv : process clustering of columns or rows (default TRUE to both)


1 Answers

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)
like image 117
IRTFM Avatar answered Nov 15 '22 05:11

IRTFM