r - 热图返回错误 : 'x' must be a numeric matrix, 但 x 是数字矩阵

标签 r error-handling heatmap numeric data-science

我正在尝试创建六个地点的物种丰度热图。 我有一个由地点与物种、数值丰度数据组成的矩阵。

但是,当我运行代码时,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'

最佳答案

有两个问题:

  1. 第一列 log_mean_wide$Site 是非数字。
  2. 热图仅接受矩阵作为输入数据(而不是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\")")

enter image description here

关于r - 热图返回错误 : 'x' must be a numeric matrix, 但 x 是数字矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50097524/

相关文章:

r - 稀疏矩阵的内存高效创建

r - 在 R 中的 flextable 包中将数字列格式化为百分比

vba - 出错时退出子程序并将错误返回给调用错误子程序的子程序

python - 如何在 Python 中高效计算两个高斯分布的热图?

python - 如何使用Bokeh正确创建HeatMap

r - quanteda kwic 提取数字后跟百分比

r - 在R中找到网格上的点?

php - 包含的PHP @运算符不会禁用error_reporting

php - 引用 - 这个错误在 PHP 中意味着什么?

ios - 移动 iOS 使用跟踪