pheatmap:NA的颜色

dar*_*ber 12 r na pheatmap

使用R package pheatmap绘制热图.有没有办法为输入矩阵中的NA分配颜色?看来NA默认为白色.例如:

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)
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 11

其实,现在问题很简单了。当前的 pheatmap 函数已合并了一个用于为“NA”分配颜色的参数 na_col。例子:

na_col = "grey90"
Run Code Online (Sandbox Code Playgroud)


nic*_*ico 10

这是可能的,但需要一些黑客攻击.

首先让我们看看如何pheatmap绘制热图.您可以通过键入pheatmap控制台并滚动输出来检查,或者使用edit(pheatmap).

你会发现使用颜色进行映射

mat = scale_colours(mat, col = color, breaks = breaks)
Run Code Online (Sandbox Code Playgroud)

scale_colours函数似乎是pheatmap包的内部函数,但我们可以使用查看源代码

getAnywhere(scale_colours)
Run Code Online (Sandbox Code Playgroud)

这使

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))))
}
Run Code Online (Sandbox Code Playgroud)

现在我们需要检查scale_vec_colours,结果是:

function (x, col = rainbow(10), breaks = NA) 
{
    return(col[as.numeric(cut(x, breaks = breaks, include.lowest = T))])
}
Run Code Online (Sandbox Code Playgroud)

因此,基本上,用于决定pheatmap使用cut哪种颜色.

让我们试着看看cut周围是否有NAs:

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
Run Code Online (Sandbox Code Playgroud)

它返回NA!那么,这是你的问题!

现在,我们如何解决它?最简单的方法是pheatmap绘制热图,然后按照我们的喜好过度绘制NA值.

pheatmap看看你会看到的功能,它会使用grid包来绘图(另请参阅这个问题:R - 如何在pheatmap中添加线条和文字?)

因此,您可以使用grid.rect矩形添加到NA位置.我要做的是通过反复试验找到热图边界的坐标,然后从那里开始绘制矩形.

例如:

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 were found by trial and error
# They WILL be different on your system and will vary when you change
# the size of the output, you may want to take that into 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))

for (x in seq_along(coord.x))
  {
  for (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"))    
    }
  }
Run Code Online (Sandbox Code Playgroud)

一个更好的解决方案是破解pheatmap使用该edit函数的代码,并让它按照您的意愿处理NA ...


Mar*_*kus 7

您可以使用github中的pheatmap的开发人员版本来启用颜色分配.你可以使用devtools来做到这一点:

#this part loads the dev pheatmap package from github
if (!require("devtools")) {
  install.packages("devtools", dependencies = TRUE)
  library(devtools)
}
install_github("raivokolde/pheatmap")
Run Code Online (Sandbox Code Playgroud)

现在您可以在pheatmap函数中使用参数"na_col":

pheatmap(..., na_col = "grey", ...)
Run Code Online (Sandbox Code Playgroud)

(编辑)不要忘记之后加载它.安装后,您可以将其视为任何其他已安装的软件包.