r - pheatmap: Color for NA -
using r package pheatmap draw heatmaps. there way assign color nas in input matrix? seems na gets colored in white default. e.g.:
library(pheatmap) m<- matrix(c(1:100), nrow= 10) m[1,1]<- na m[10,10]<- na pheatmap(m, cluster_rows=false, cluster_cols=false) thanks
it possible, requires hacking.
first of let's see how pheatmap draws heatmap. can check typing pheatmap in console , scrolling through output, or alternatively using edit(pheatmap).
you find colours mapped using
mat = scale_colours(mat, col = color, breaks = breaks) the scale_colours function seems internal function of pheatmap package, can check source code using
getanywhere(scale_colours) which gives
function (mat, col = rainbow(10), breaks = na)  {     mat = as.matrix(mat)     return(matrix(scale_vec_colours(as.vector(mat), col = col,          breaks = breaks), nrow(mat), ncol(mat), dimnames = list(rownames(mat),          colnames(mat)))) } now need check scale_vec_colours, turns out be:
function (x, col = rainbow(10), breaks = na)  {     return(col[as.numeric(cut(x, breaks = breaks, include.lowest = t))]) } so, essentially, pheatmap using cut decide colours use.
let's try , see cut if there nas around:
as.numeric(cut(c(1:100, na, na), seq(0, 100, 10)))   [1]  1  1  1  1  1  1  1  1  1  1  2  2  2  2  2  2  2  2  2  2  3  3  3  3  3  3  3  3  [29]  3  3  4  4  4  4  4  4  4  4  4  4  5  5  5  5  5  5  5  5  5  5  6  6  6  6  6  6  [57]  6  6  6  6  7  7  7  7  7  7  7  7  7  7  8  8  8  8  8  8  8  8  8  8  9  9  9  9  [85]  9  9  9  9  9  9 10 10 10 10 10 10 10 10 10 10 na na it returns na! so, here's issue!
now, how around it? easiest thing let pheatmap draw heatmap, overplot na values like.
looking again @ pheatmap function you'll see uses grid package plotting (see question: r - how add lines , text pheatmap?)
so can use grid.rect add rectangles na positions. find coordinates of heatmap border trial , error, work there plot rectangles.
for instance:
library(pheatmap) m<- matrix(c(1:100), nrow= 10) m[1,1]<- na m[10,10]<- na  hmap <- pheatmap(m, cluster_rows=false, cluster_cols=false) # these values found trial , error # different on system , vary when change # size of output, may want take account. min.x <- 0.005 min.y <- 0.01 max.x <- 0.968 max.y <- 0.990 width <- 0.095 height <- 0.095  coord.x <- seq(min.x, max.x-width, length.out=ncol(m)) coord.y <- seq(max.y-height, min.y, length.out=nrow(m))  (x in seq_along(coord.x))   {   (y in seq_along(coord.y))     {     if (is.na(m[x,y]))         grid.rect(coord.x[x], coord.y[y], just=c("left", "bottom"),                   width, height, gp = gpar(fill = "green"))         }   } a better solution hack code of pheatmap using edit function , have deal nas wish...
Comments
Post a Comment