我正在尝试创建六个地点的物种丰度热图。 我有一个由地点与物种、数值丰度数据组成的矩阵。
但是,当我运行代码时,R 返回一个错误,指出我的矩阵是非数字的。 有人能解决这个问题吗?我被难住了。
导出的数据帧链接:log_mean_wide
工作:
lrc <- rainbow(nrow(log_mean_wide), start = 0, end = .3)
lcc <- rainbow(ncol(log_mean_wide), start = 0, end = .3)
logmap <- heatmap(log_mean_wide, col = cm.colors(256), scale = "column",
RowSideColors = lrc, ColSideColors = lcc, margins = c(5, 10),
xlab = "species", ylab = "Site",
main = "heatmap(<Auckland Council MCI data 1999, habitat:bank>, ..., scale = \"column\")")
error message: Error in heatmap(log_mean_wide, Rowv = NA, Colv = NA, col = cm.colors(256), : 'x' must be a numeric matrix
log_heatmap <- heatmap(log_mean_wide, Rowv=NA, Colv=NA, col = cm.colors(256), scale="column", margins=c(5,10)) #same error
is.numeric(log_mean_wide) #[1] FALSE
is.character(log_mean_wide) #[1] FALSE
is.factor(log_mean_wide) #[1] FALSE
is.logical(log_mean_wide) #[1] FALSE
is.integer(log_mean_wide) #[1] FALSE
?!?!
dims <- dim(log_mean_wide)
log_mean_matrix <- as.numeric(log_mean_wide)
dim(log_mean_matrix) <- dims
Error: (list) object cannot be coerced to type 'double'
str(log_mean_wide) 将物种显示为数字,将站点显示为字符 - 为什么这不起作用?
storage.mode(log_mean_wide) <- "numeric"
Error in storage.mode(log_mean_wide) <- "numeric" : (list) object cannot be coerced to type 'double'
最佳答案
有两个问题:
- 第一列
log_mean_wide$Site
是非数字。 热图
仅接受矩阵
作为输入数据(而不是data.frame
)。
要解决这些问题,您可以执行以下操作(请注意,热图中有很多困惑):
# Store Site information as rownames
df <- log_mean_wide;
rownames(df) <- log_mean_wide[, 1];
# Remove non-numeric column
df <- df[, -1];
# Use as.matrix to convert data.frame to matrix
logmap <- heatmap(
as.matrix(df),
col = cm.colors(256),
scale = "column",
margins = c(5, 10),
xlab = "species", ylab = "Site",
main = "heatmap(<Auckland Council MCI data 1999, habitat:bank>, ..., scale = \"column\")")
关于r - 热图返回错误 : 'x' must be a numeric matrix, 但 x 是数字矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50097524/