R package heatmap.plus ColSideColors参数给出尺寸误差

时间:2014-10-13 10:24:47

标签: r heatmap

我需要绘制带注释的矩阵的热图,因此使用heatmap.plus R包我需要使用ColSideColors参数。这里的问题是它要求相同的尺寸,即使它们是相同的......

> m <- matrix(rnorm(100,1, 20), 10, 10)
> c <- t(as.matrix(rep('gold', 10), ncol=10, nrow=10))
> heatmap.plus(m, ColSideColors=c)
Error in heatmap.plus(m, ColSideColors = c) : 
  'ColSideColors' dim()[2] must be of length ncol(x)
> dim(c)[2]
[1] 10
> ncol(m)
[1] 10

更新 在以下代码的情况下是什么?

> m <- matrix(rnorm(100,1, 20), 10, 10)
> c <- t(as.matrix(cbind(rep('gold', 10), rep('blue', 10)), ncol=2, nrow=10))
> heatmap.plus(m, ColSideColors=c)
Error in heatmap.plus(m, ColSideColors = c) : 
  'ColSideColors' dim()[2] must be of length ncol(x)
> dim(c)[2]
[1] 10
> ncol(m)
[1] 10

换句话说,当我想从矢量构建矩阵时我该怎么办??

1 个答案:

答案 0 :(得分:2)

我认为你的c导致了这个问题。你的c是1(行)×10(列)的矩阵。但是,heatmap.plus期待10行。按照你的例子,这就是我所做的。

m <- matrix(rnorm(100, 1, 20), 10, 10)
c <- matrix("gold", ncol = 10, nrow = 10)

heatmap.plus(m, ColSideColors=c)

enter image description here

我按照CRAN手册中的示例进行了操作,并执行了以下操作。如有需要,请看一下。

m = matrix(rnorm(100,1, 20), 10, 10)
rlab = matrix(c("gold", "green", "blue", "red"), nrow = 10, ncol = 4)
clab = matrix(c("green", "blue"), nrow = 10, ncol =2)
colnames(rlab) = LETTERS[1:dim(rlab)[2]]
colnames(clab) = 1:dim(clab)[2]

heatmap.plus(m,ColSideColors=clab,RowSideColors=rlab)

enter image description here