pheatmap:NA的颜色

时间:2014-09-19 08:52:57

标签: r na pheatmap

使用R package pheatmap绘制热图。有没有办法为输入矩阵中的NA分配颜色?看来NA默认为白色。 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)

由于

5 个答案:

答案 0 :(得分:10)

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

首先让我们看看pheatmap如何绘制热图。您只需在控制台中输入pheatmap并滚动输出,或者使用edit(pheatmap)来检查即可。

您会发现使用

映射颜色
mat = scale_colours(mat, col = color, breaks = breaks)

scale_colours函数似乎是pheatmap包的内部函数,但我们可以使用

检查源代码
getAnywhere(scale_colours)

哪个给出了

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))))
}

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

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

因此,基本上pheatmap正在使用cut来决定使用哪种颜色。

如果周围有NAs,请尝试查看cut的作用:

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

它返回NA!所以,这是你的问题!

现在,我们如何解决这个问题? 最简单的方法是让pheatmap绘制热图,然后根据需要重写NA值。

再次查看pheatmap功能,您会看到它使用grid包进行绘图(另请参阅此问题:R - How do I add lines and text to 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"))    
    }
  }

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

答案 1 :(得分: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")

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

pheatmap(..., na_col = "grey", ...)

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

答案 2 :(得分:1)

如果您不介意使用heatmap.2中的gplots,那么就会有一个方便的na.color参数。从上面获取示例数据m

library(gplots)
heatmap.2(m, Rowv = F, Colv = F, trace = "none", na.color = "Green")

答案 3 :(得分:1)

如果您希望NA为灰色,则可以简单地将“NA”强制为加倍。

m[is.na(m)] <- as.double("NA")
pheatmap(m, cluster_rows=F, cluster_cols=F)

答案 4 :(得分:1)

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

na_col = "grey90"