热图返回错误:' x'必须是数字矩阵,但x是数字矩阵

时间:2018-04-30 09:22:25

标签: 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\")")
  

错误消息:热图中的错误(log_mean_wide,Rowv = NA,Colv = NA,col = cm.colors(256),:&#39; x&#39;必须是数字矩阵

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
  

错误:(列表)对象无法强制键入&#39; double&#39;

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

storage.mode(log_mean_wide) <- "numeric" 
  

storage.mode(log_mean_wide)中的错误&lt; - &#34;数字&#34; :( list)对象无法强制键入&#39; double&#39;

1 个答案:

答案 0 :(得分:0)

有两个问题:

  1. 第一列log_mean_wide$Site是非数字的。
  2. heatmap仅接受matrix作为输入数据(不是data.frame)。
  3. 要解决这些问题,您可以执行以下操作(请注意,热图中有很多的混乱):

    # 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