user3810226 - 1 year ago 64
R Question

# Modification to heat map

I am making a heat map though would like to separate the columns and add a line between each row. I am well aware that doing so makes this well, not a heat map. But this is how my boss envisions it.

Below is my code for the current heat map. Any advice on separating the columns & adding a line between each "person" would be much appreciated.

``````x11 <- c(0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1)
x22 <- c(1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1)
x <- rbind(x11, x22)
hv <- heatmap(t(x), col = c("cornflowerblue", "hotpink"), margins = c(4, 12), Colv = NA, Rowv = NA, scale = "none", xlab ="", ylab ="", main = "",  labCol=c("BP", "Cx"), cexCol =2)
legend("topright", c("No Osteomyelitis", "Osteomyelitis"), col=c("cornflowerblue", "hotpink"), bty="n", fill=c("cornflowerblue", "hotpink"))
``````

I suggest you swap to `gplots::heatmap.2()` which allows greater control over plotting with mostly the same arguments.

Building on your good example (+1 btw) by adding the `colsep`, `rowsep`, `sepcolor` and `sepwidth` arguments to control the separation between the rows and columns (and `trace = 'none'` because I don't like it) gives:

``````x11 <- c(0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1)
x22 <- c(1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1)
x <- rbind(x11, x22)
hv <- heatmap(t(x), col = c("cornflowerblue", "hotpink"), margins = c(4, 12), Colv = NA, Rowv = NA, scale = "none", xlab ="", ylab ="", main = "",  labCol=c("BP", "Cx"), cexCol =2)
legend("topright", c("No Osteomyelitis", "Osteomyelitis"), col=c("cornflowerblue", "hotpink"), bty="n", fill=c("cornflowerblue", "hotpink"))

library(gplots)
heatmap.2(t(x),
col = c("cornflowerblue", "hotpink"),
margins = c(4, 12),
Colv = NA, Rowv = NA,
scale = "none",
xlab ="",
ylab ="",
main = "",
labCol=c("BP", "Cx"),
cexCol = 2,
trace = 'none',
colsep = seq(1,nrow(x)-1),
rowsep = seq(1,ncol(x)-1),
sepcolor = "white",
sepwidth = c(0.1, 0.05))
``````

To separate the columns more, increse the first element of `sepwidth` and similarly for the rows.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download