Question: Multi-Panel Heatmaps In R
gravatar for lkmklsmn
5.9 years ago by
United States
lkmklsmn890 wrote:

does anyone know of a package/hack to draw multipanel heatmaps in R?


R heatmap • 15k views
ADD COMMENTlink modified 5.9 years ago by seidel6.8k • written 5.9 years ago by lkmklsmn890

Hi lkmklsmn.

What do you mean by multipanel heatmaps? I generally use gplots' heatmap.2.

ADD REPLYlink modified 5.9 years ago • written 5.9 years ago by Leandro Lima930

That what I ended up using, but I would rather make separate heatmaps. I know you can add 'rowsep' or 'colsep', but they draw over the heatmap and dont actually move the two heatmaps away from each other.

ADD REPLYlink written 5.9 years ago by lkmklsmn890

Could you link to an example/picture of a multipanel heatmap?

ADD REPLYlink written 5.9 years ago by Ying W3.9k

For example fig3 from this paper:

ADD REPLYlink written 5.9 years ago by lkmklsmn890

I would guess that they made 4 different heatmaps keeping the axis consistent then trimmed off the axis/labels and then put it together in photoshop

ADD REPLYlink written 5.9 years ago by Ying W3.9k
gravatar for seidel
5.9 years ago by
United States
seidel6.8k wrote:

One way to draw several heat maps together would be to use the layout() function along with image(). Layout (type ?layout for help) is a way to divide the graphics device up for arranging plots. The image() function is a generic way of drawing heat map grids. Thus you could use the regular heatmap() function to easily get your row and column orderings for your heat maps, but then redraw them using layout() and image(). For example:

# for an example heat map
some_random_data <- matrix(rnorm(100), nrow=10,ncol=10)
hm1 <- heatmap(some_random_data)

# hm1$rowInd now contains the row ordering of your data in the heat map image

If you had 4 such heat maps that you want to create in an arrangement, use layout() to divide up the graphics device. The first argument is a matrix reflecting the order to draw the plots, the second argument has to do with the widths and heights of the rows and columns. It's a bit confusing, but read the documentation and experiment:

# create a layout to draw 4 heatmaps, 2 will be big and 2 will be small
nf <- layout(matrix(c(1,2,3,4),2,2, byrow=T), widths=c(2,2), heights=c(3,1), respect=T)
# check how the layout looks

# now draw 4 heat maps

That's the basic idea. It looks ugly at first because of the default margins etc., but then you can start cleaning it up:

layout(matrix(c(1,2,3,4),2,2, byrow=T), widths=c(2,2), heights=c(3,1), respect=T)
# adjust the margins
# turn off the axes
image(matrix(rnorm(100),10,10), axes=FALSE)
image(matrix(rnorm(100),10,10), axes=FALSE)
image(matrix(rnorm(50),10,5), axes=FALSE)
image(matrix(rnorm(50),10,5), axes=FALSE)

The last caveat is that when you give image() a matrix of numbers, it transposes it. Thus to draw your heat map the way you see it in heatmap(), you'd have to do something like the following:


which transposes your data, then puts the rows in the heatmap order, then image() draws it. You can add labels, and axes, and titles, and make legends, etc. all using typical R plotting stuff. Igt just takes a while to figure it all out.

ADD COMMENTlink written 5.9 years ago by seidel6.8k

Though it is a bit late (just ~3y after this was posted), I still want to say thanks. This is really helpful!

ADD REPLYlink written 3.1 years ago by yupeng he80
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1992 users visited in the last hour