I'm currently working on a heatmap that uses a complex discrete color scale (about 34 different colors). Some of the colors are dark while others are light. I'm labeling cells with the cellnote
argument of the heatmap.2()
function. Since some of the background colors of the cells are very dark, when using a black cellnote color the cell labels can't be seen. Likewise, when using a lighter cellnote color the labels in the cells with a light background color can't be seen. Was wondering if it is possible to have different cellnote colors in a single heatmap produced with the ´heatmap.2()´ function. Here is a small example of the situation:
library(gplots)
x <- cbind(c(0.5, 1.5), c(1.5, 0.5))
pal <- c("darkblue", "lightblue")
heatmap.2(
x,
Rowv = FALSE,
Colv = FALSE,
dendrogram = "none",
key = FALSE,
trace = "none",
col = pal,
breaks = seq(0, 2),
cellnote = x,
notecol = "black",
notecex = 2
)
The desired output would be to have white cellnote in the upper-left and down-right cells while keeping a black cellnote in the other cells. I anticipate this will potentially require to modify the function's source code. However, I haven't been able to come up with a solution myself.
Well... apparently you can pass a matrix to the notecol
argument lol. I assumed this wasn't possible initially because in the documentation it said 'string' but well my bad for not trying before. However, for some reason I seem to have to reverse the columns of the color character matrix to actually get the desired result? This is kinda weird but it works:
notemat <- cbind(c("white", "black"), c("black", "white"))
heatmap.2(
x,
Rowv = FALSE,
Colv = FALSE,
dendrogram = "none",
key = FALSE,
trace = "none",
col = pal,
breaks = seq(0, 2),
cellnote = x,
notecol = apply(notemat, 2, rev),
notecex = 2
)