user2380782 user2380782 - 1 month ago 12
R Question

draw cell borders using heatmap.2

I'm trying to make a heatmap using

heatmap.2
but I don't get a cell border. If I set the parameter
sepwidth
and
sepcolor
it does not work, I have to include the
colsep
and
rowsep
parameters but still doing that, some cell borders are not drawn, any ideas?

heatmap.2(as.matrix(df), key=F, trace="none", ColSideColors=colorside,
cexRow=0.6, breaks=bk1, col=colors2,
lmat=rbind(c(0,0), c(0,4), c(0,1), c(3,2), c(0,0)),
lhei=c(0.4,0.3,0.05,0.4,0.6),
sepwidth=c(0.01, 0.01), sepcolor="black",
colsep=1:length(df), rowsep=1:length(df))

Answer

It appears your problem is with the colsep and rowsep arguments. From the help file:

colsep, rowsep, sepcolor (optional) vector of integers indicating which columns or rows should be separated from the preceding columns or rows by a narrow space of color sepcolor

Instead of "indicating which columns or rows should be separated", your code creates a vector as long as the number of elements in the matrix. If you had set colsep=c(1,3), the separator between the 1st and 2rd columns and the separator between the 3rd and 4th columns would have been colored. I don't believe there is a way to color the cell borders without using the colsep and rowsep arguments. The cell borders are, by default, not drawn unless these arguments are given values.

# First, a reproducible data set
library(gplots)
mat = matrix( rnorm(100), ncol=5 )
colorside = gray(1:5/5)
bk1 = seq(min(mat),max(mat),length.out=11)
col = redgreen(10)

# And now the heatmap
heatmap.2( mat, 
           key=FALSE, 
           trace="none",
           ColSideColors=colorside,
           cexRow=0.6,
           breaks=bk1,
           col=col,
           sepwidth=c(0.1,0.1),
           sepcolor="purple",
           colsep=1:ncol(mat),
           rowsep=1:nrow(mat))

enter image description here