我在gplots中使用heatmap.2在R中创建热图。我的代码用于创建热图。但最近它开始抛出错误。我不确定为什么会这样。
# Matrix to feed heatmap.2
mat = as.matrix(sort_pkmat)
# color palette for diff. colors
my_palette <- colorRampPalette(c("white","red","red4"))(n = 299)
# color breaks for range
col_breaks = c(seq(0,40,length=100),
seq(40,60,length=100),
seq(60,90,length=100))
path1 = paste(path,name,'.png', sep = '')
print(path1)
png(path1,
width = 10*300, # 5 x 300 pixels
height = 8*300,
res = 300, # 300 pixels per inch
pointsize = 8)
heatmap.2(mat,
#cellnote = mat, # same data set for cell labels
main = "Tag_density_HeatMap", # heat map title
xlab = "Peaks",
ylab = "Chip_samples",
labRow = FALSE,
labCol = FALSE,
cexRow = 0.7, # Changes the size of col and row font size
cexCol = 0.2,
notecol="black", # change font color of cell labels to black
density.info="none", # turns off density plot inside color legend
trace="none", # turns off trace lines inside the heat map
margins =c(3,5), # widens margins around plot
col=my_palette, # use on color palette defined earlier
breaks=col_breaks, # enable color transition at specified limits
dendrogram="none", # only draw a row dendrogram
Colv= FALSE, #cluster column
Rowv = FALSE,
#keysize = 1
)
dev.off()
现在它抛出错误:
Error in seq.default(min.raw, max.raw, by = min(diff(breaks)/4)) :
invalid (to - from)/by in seq(.)
> dev.off()
null device
1
此代码绘制热图,但不是颜色键。
答案 0 :(得分:2)
我使用heatmap.2遇到同样的问题。它似乎是由diff(break)返回的向量中有一个或多个零值产生的。当min(diff(break))为零时,'by'调用失败。我将断点更改为非重叠,并且可以显示颜色键。
最初我有:break = c(seq(0,0.033,length = 25),seq(0.033,0.066,length = 26),seq(0.066,0.1,length = 25))
我将其更改为: 符= C(SEQ(0,0.033,长度= 25),SEQ(0.0331,0.066,长度= 26),SEQ(0.0661,0.1,长度= 25))
它似乎解决了这个问题,并允许我再次使用颜色键。
答案 1 :(得分:1)
我遇到了同样的问题。我能够用钥匙制作一个情节,但没有我喜欢的控制:
我删除了&#34;休息&#34;完全选项并使用col = redblue(64)并生成带有键的双色热图。
答案 2 :(得分:1)
我遇到了以下问题,得到了上面的例子:
x = matrix(runif(100^2, min=0, max=100), ncol=100)
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 99) # 100 percent
col_breaks = c(seq(0,10,length=40),
seq(11,40,length=30),
seq(41,100,length=30))
hm <- heatmap.2(x,
col=my_palette,
breaks=col_breaks,
dendrogram="none",
Colv="NA", Rowv="NA")
生成此热图:
但是如果我在矩阵中引入一个负数,然后修改向量中的中断以解决这个负数,即
x = matrix(runif(100^2, min=-1, max=100), ncol=100)
col_breaks = c(seq(-1,10,length=40),
seq(11,40,length=30),
seq(41,100,length=30))
hm <- heatmap.2(x,
col=my_palette,
breaks=col_breaks,
dendrogram="none",
Colv="NA", Rowv="NA")
密钥完全失败:
在this answer之后添加simkey=FALSE
解决了此问题,并且在此示例中似乎可行:
hm <- heatmap.2(x,
col=my_palette,
breaks=col_breaks,
symkey=FALSE,
dendrogram="none",
Colv="NA", Rowv="NA")
如您所见:
答案 3 :(得分:1)
实际上我也遇到过这个问题,需要结合以上两个答案来结束工作代码。
col_breaks = c(seq(-3,-1,length=100), # for red
seq(-0.99,0.99,length=100), # for yellow
seq(1,3,length=100)) # for green
注意:调用 heatmap.2() 函数时不需要添加 simkey=FALSE
。
答案 4 :(得分:0)
使用相同的断点(在我的例子中分别为10和40两次)给出了这个错误,即:
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 99) # 100 percent
col_breaks = c(seq(0,10,length=40),
seq(10,40,length=30),
seq(40,100,length=30))
带来了这个错误,而
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 99) # 100 percent
col_breaks = c(seq(0,10,length=40),
seq(11,40,length=30),
seq(41,100,length=30))
解决了以下错误:
x = matrix(runif(100^2, min=0, max=100), ncol=100)
hm <- heatmap.2(x,
col=my_palette,
breaks=col_breaks,
dendrogram="none",
Colv="NA", Rowv="NA")