将heatmap.2保存在变量中并再次绘图

时间:2013-05-13 09:17:15

标签: r heatmap gplots

我使用gplots中的heatmap.2制作热图:

library(gplots)
# some fake data
m = matrix(c(0,1,2,3), nrow=2, ncol=2)
# make heatmap
hm = heatmap.2(m)

当我直接进行'heatmap.2'时,我会得到一个可以输出到设备的图。如何从我的变量'hm'再次制作情节?显然这是一个玩具示例,在现实生活中我有一个生成并返回热图的函数,我想稍后绘制。

2 个答案:

答案 0 :(得分:4)

有几种选择,虽然它们都不是特别优雅。这取决于您的函数使用的变量是否在绘图环境中可用。 heatmap.2 不返回正确的“热图”对象,尽管它包含再次绘制图形的必要信息。请参阅 str(hm)以检查对象。

如果您的环境中有变量,您可以重新评估原始绘图调用:

library(gplots)
# some fake data (adjusted a bit)
set.seed(1)
m = matrix(rnorm(100), nrow=10, ncol=10)
# make heatmap
hm = heatmap.2(m, col=rainbow(4))

# Below fails if all variables are not available in the global environment
eval(hm$call)

我认为这不会是这种情况,因为你提到你从函数内部调用plot命令,我认为你没有使用任何全局变量。您可以从 hm 对象中可用的字段重新构建热图绘制调用。问题是原始矩阵不可用,而是我们重新组织了 $ carpet -field。它需要一些修补才能获得原始矩阵,因为投影是:

# hm2$carpet = t(m[hm2$rowInd, hm2$colInd])

至少在未缩放数据矩阵的情况下,以下内容应该有效。根据您的特定绘图调用添加额外参数。

func <- function(mat){
    h <- heatmap.2(mat, col=rainbow(4))
    h
}

# eval(hm2$call) does not work, 'mat' is not available
hm2 <- func(m)

# here hm2$carpet = t(m[hm2$rowInd, hm2$colInd])
# Finding the projection back can be a bit cumbersome:
revRowInd <- match(c(1:length(hm2$rowInd)), hm2$rowInd)
revColInd <- match(c(1:length(hm2$colInd)), hm2$colInd)
heatmap.2(t(hm2$carpet)[revRowInd, revColInd], Rowv=hm2$rowDendrogram, Colv=hm2$colDendrogram, col=hm2$col)

此外,我认为您可以在功能环境中评估 hm $ call 。也许 with -function会很有用。

您也可以通过将 mat 附加到全局环境来使 mat 可用,但我认为这被认为是不好的做法,因为过于急切地使用 attach 会导致问题。请注意,在我的示例中,每次调用 func 都会创建原始图。

答案 1 :(得分:3)

我会做一些函数式编程:

create_heatmap <- function(...) {
    plot_heatmap <- function() heatmap.2(...)
}

data = matrix(rnorm(100), nrow = 10)

show_heatmap <- create_heatmap(x = data)

show_heatmap()

通过...传递您需要发送到plot_heatmap的所有参数。外部函数调用设置一个环境,其中内部函数首先查找其参数。内部函数作为对象返回,现在完全可移植。这应该每次产生完全相同的情节!

相关问题