我对R非常陌生,我有一个矩阵数据集,我想把它画成一个六角形的彩色地图。我遵循了本页概述的代码:
https://www.visualcinnamon.com/2013/11/how-to-create-hexagonal-heatmap-in-r
但是,我一直收到错误消息:
seq.default(min(x,na.rm = T),max(x,na.rm = T),length = length(ColRamp))中的错误:“from”必须是一个有限的数字,此外:警告消息: seq.default(min(x,na.rm = T),max(x,na.rm = T),length = length(ColRamp)):NAs
我的代码如下:
library(RColorBrewer)
library(fields)
Hexagon <- function (x, y, unitcell = 1, col = col) {
polygon(c(x, x, x + unitcell/2, x + unitcell, x + unitcell,
x + unitcell/2), c(y + unitcell * 0.125,
y + unitcell * 0.875,
y + unitcell * 1.125,
y + unitcell * 0.875,
y + unitcell * 0.125,
y - unitcell * 0.125),
col = col, border=NA)}
Heatmap_Matrix = as.matrix("
3 2 3 4 5 5 5 5
4 2 3 4 5 5 5 5
5 0 0 4 5 5 5 5
7 2 3 8 8 8 8 8
1 2 3 4 5 5 5 5
6 0 3 4 5 5 5 5
1 2 3 9 5 5 5 5
1 2 3 4 7 7 7 7
8 2 4 4 5 5 5 5
1 0 8 4 3 3 3 3
1 0 3 5 5 5 5 5
9 2 3 4 5 5 5 5
1 2 4 4 5 5 5 5
1 0 3 4 2 2 2 2
9 2 3 4 5 5 5 5")
x <- as.vector(Heatmap_Matrix)
SOM_Rows <- dim(Heatmap_Matrix)[1]
SOM_Columns <- dim(Heatmap_Matrix)[2]
par(mar = c(0.4, 2, 2, 7))
plot(0, 0, type = "n", axes = FALSE, xlim=c(0, SOM_Columns),
ylim=c(0, SOM_Rows), xlab="", ylab= "", asp=1)
ColRamp <- rev(designer.colors(n=50, col=brewer.pal(9, "Spectral")))
ColorCode <- rep("#FFFFFF", length(x)) #default is all white
Bins <- seq(min(x, na.rm=T), max(x, na.rm=T), length=length(ColRamp))
for (i in 1:length(x))
if (!is.na(x[i])) ColorCode[i] <- ColRamp[which.min(abs(Bins-x[i]))]
offset <- 0.5
for (row in 1:SOM_Rows) {
for (column in 0:(SOM_Columns - 1))
Hexagon(column + offset, row - 1, col = ColorCode[row + SOM_Rows * column])
offset <- ifelse(offset, 0, 0.5)
}
image.plot(legend.only=TRUE, col=ColRamp, zlim=c(min(x, na.rm=T), max(x, na.rm=T)))我对R编码不太了解,但我已经尝试了好几天来删除错误信息。可能很简单,但我不知道。我基本上只是从上面的链接中复制了代码。
如果有人知道错误是什么,我会非常感激的。
发布于 2019-11-15 13:59:52
我认为这是关于如何输入Heatmap_Matrix的。当我运行str(x)时,我看到它把它读成一个长串字符(这是引号告诉它的)。用逗号分隔数值是我通常的方法,或者如果是大量的数据,将其保存为标签或逗号分隔的文本文件,例如excel,然后用read.csv等在R中读取它。
https://stackoverflow.com/questions/58877941
复制相似问题