简体   繁体   中英

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

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:

在此输入图像描述

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")

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))

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM