Heatmap error: 'x' must be a numeric matrix
2
3
Entering edit mode
3.1 years ago

Hi everyone

I'm making heatmap by using r studio. My data have 4 rows, 1 with name and 3 with values. I export excel sheet by selecting numeric.

library(readxl)
drug_class <- read_excel("C:/Users/hondalab/OneDrive/Desktop/16s NTR/Hiseq/drug_class.xlsx", 
    col_types = c("text", "numeric", "numeric", 
        "numeric"))

When I put heatmap command but I got this error.

heatmap(drug_class)
Error in heatmap(drug_class) : 'x' must be a numeric matrix

print command showed this;

> print(drug_class)
# A tibble: 29 x 4
   `drug class`                   `NTR 1 Rain` `NTR 2 Dry` `NTR 2 Rain`
   <chr>                                 <dbl>       <dbl>        <dbl>
 1 aminocoumarin                       0.00233    0.0239      0.00393  
 2 aminoglycoside                      0.00559    0.136       0.0725   
 3 antibacterial free fatty acids      0          0           0.0000264
 4 benzalkonium chloride               0          0.000615    0.000123 
 5 carbapenem                          0.00255    0.0321      0.0184   
 6 cephalosporin                       0.00444    0.0644      0.0485   
 7 cephamycin                          0.00240    0.0264      0.00830  
 8 diaminopyrimidine                   0.00244    0.0324      0.0412   
 9 fluoroquinolone                     0.00637    0.0528      0.0317   
10 fosfomycin                          0          0           0.000412 
# ... with 19 more rows

Can anybody help to solve this error. Thanks!

R heatmap • 11k views
ADD COMMENT
2
Entering edit mode

Your first column is numeric and you need to convert it as row names or you can also skip this.

col_types <- as.matrix(col_types[,2:4])
ADD REPLY
0
Entering edit mode

after converting this, it' still showing same error.

ADD REPLY
0
Entering edit mode
ADD REPLY
6
Entering edit mode
3.1 years ago

heatmap says it requires a numeric matrix, so you probably have to convert your tibble to a matrix.

drug_class <- as.data.frame(drug_class)
rownames(drug_class) <- drug_class[, 1]
drug_class[, 1] <- NULL
drug_class <- as.matrix(drug_class)

Tidyverse ansywer

library("tidyverse")

drug class <- drug_class %>%
  column_to_rownames("drug class") %>%
  as.matrix
ADD COMMENT
0
Entering edit mode

Thanks. it works.

ADD REPLY
5
Entering edit mode
3.1 years ago

can you try following?

heatmap(as.matrix(drug_class[,2:ncol(drug_class)]))

you can also try:

library(data.table)
heatmap(as.matrix(as.data.table(iris),rownames = 1))

Instead of default heatmap, I would suggest pheatmap and complexheatmap packages.

ADD COMMENT

Login before adding your answer.

Traffic: 3016 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6