【问题标题】:Heatmap returning error: 'x' must be a numeric matrix, but x is a numeric matrix热图返回错误:“x”必须是数字矩阵,但 x 是数字矩阵
【发布时间】:2018-10-10 08:56:20
【问题描述】:

我正在尝试创建六个地点的物种丰度热图。 我有一个地点与物种的矩阵,包含数字丰度数据。

但是,当我运行我的代码时,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\")")

错误信息:热图中错误(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

错误:(列表)对象不能被强制输入'double'

str(log_mean_wide) 将物种显示为数字,站点显示为字符 - 为什么这不起作用?

storage.mode(log_mean_wide) <- "numeric" 

storage.mode(log_mean_wide) 中的错误

【问题讨论】:

    标签: r error-handling heatmap numeric data-science


    【解决方案1】:

    有两个问题:

    1. 第一列log_mean_wide$Site 是非数字的。
    2. heatmap 只接受 matrix 作为输入数据(不是 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\")")
    

    【讨论】:

    • 非常感谢。我有一种感觉,因为有这么多的物种……回到绘图板。它最初是一个矩阵,但我将它导出为数据框。当我运行此代码时,我收到错误消息:不推荐在 tibble 上设置行名称。row.names&lt;-.data.frame(*tmp*, value = value) 中的错误:无效的 'row.names' 长度 你明白了吗?不确定如何解决。
    • @chloep 不,我没有看到那个错误。您必须有一些额外的代码导致此问题,因为在我的解决方案中,我没有使用 tibbles 或任何 tidyverse 函数。我刚刚以log_mean_wide &lt;- read.csv("log_mean_wide.csv") 下载并读取了您的数据。
    • @chloep PS关于混乱:也许过滤colMeans超过某个阈值的列(物种)?您可以看到有一个大的白色区域,其中值基本上全为零。或者定义一些列排名指标来选择前 10 个物种?
    • 是的,这将是一个很好的方法 - 将解决这个问题,一旦可以解决这个问题!成功后将使用我的代码更新..
    • @chloep 祝你好运;-)
    猜你喜欢
    • 2019-01-08
    • 2021-12-29
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多