Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add horizontal separator in R's heatmap.2

Tags:

plot

r

heatmap

With the following codes:

library(gplots)
heatmap.2(as.matrix(mtcars),density.info="none",trace="none",Colv=FALSE,Rowv=FALSE,dendrogram="none",margin=c(10,10))

I can create the heatmap. But what I want to do is to add the horizontal separators of the given blocks:

enter image description here

Let say I have 3 blocks defined as lists:

block1 <- c("Mazda RX4","Mazda RX4 Wag","Datsun 710","Hornet 4 Drive","Hornet Sportabout","Valiant","Duster 360","Merc 240D")
block2 <- c("Merc 230","Merc 280","Merc 280C","Merc 450SE","Merc 450SL","Merc 450SLC","Cadillac Fleetwood", "Lincoln Continental","Chrysler Imperial"  ,"Fiat 128","Honda Civic","Toyota Corolla","Toyota Corona","Dodge Challenger","AMC Javelin")
block3 <- c("Camaro Z28","Pontiac Firebird","Fiat X1-9","Porsche 914-2","Lotus Europa","Ford Pantera L","Ferrari Dino","Maserati Bora","Volvo 142E")
like image 447
pdubois Avatar asked Mar 25 '15 13:03

pdubois


1 Answers

You can use the parameter rowsep in heatmap.2 function (similarly, if you're interested in adding vertical lines, you can use parameter colsep).

Here, you want to put a separation after the 8th values and then after the 23rd so you can do:

heatmap.2(as.matrix(mtcars),
          density.info="none", 
          trace="none", 
          Colv=FALSE, 
          Rowv=FALSE, 
          dendrogram="none", 
          margin=c(10,10),
          # now the separations:
          rowsep=c(8, 23))

enter image description here

NB: to retrieve the places of the horizontal lines based on the block vectors, you can do

which(row.names(mtcars)==block1[length(block1)]) # 8
which(row.names(mtcars)==block2[length(block2)]) # 23
like image 196
Cath Avatar answered Oct 25 '22 04:10

Cath