Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while creating heatmaps - NA/NaN/Inf in foreign function call (arg 11)

Tags:

r

nan

na

gplots

I'm trying to prepare heatmap for my data but I have no idea why this error appears.

My data:

> dput(head(tbl_ready))
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.370330677123077, 
0, 0, 0, 0, 0, 0.53318856142826, 0, 0, 0, 0, 0, 0.217669675587482, 
0, 0, 0, 0.79337589572453, 0, 1, 0.0132525790616207, 0, 0, 1, 
0.498415470211292, 0.216961707575178, 0.0646831352678839, 0, 
0, 0, 0.778625047514492, 0.165974546372072, 0.076951015613392, 
0.889894091237216, 0, 0, 1, 0.129806153151281, 0.197647497443337, 
1, 0, 0, 0.509023013860118, 0.159412145987791, 0.207873742711735, 
0.749031133231353, 0.222918051830986, 0, 0.741479370384933, 0.133323148299248, 
0.216599753666685, 0.962652293738836, 0.303065152126049, 0, 0.801394522615822, 
0, 0.15864534869139, 0, 0.193050421324826, 0, 0.799048954936309, 
0, 0.328823938175914, 1, 0.425065664971905, 0, 0.578904125773447, 
0, 0.186228586828205, 0, 0.428641900764779, 0, 0.276948678897629, 
0, 0.117434041208573, 0, 0.272644463294893, 0, 0.138614907082177, 
0, 0.180927300758764, 0, 0.435388112571728, 0, 0, 0, 0.475087644525129, 
0, 0.762104865898499, 0, 0, 0, 0.227200985463951, 0, 0.418218667506089, 
0, 0, 0, 0.478763051110549, 0, 0.875837464800909, 0, 0, 0, 0.188429982762364, 
0, 0.418218667506089, 0, 0, 0, 0.349226871785504, 0, 0.998103388096146, 
0, 0, 0, 0.444963481341964, 0, 1, 0, 0, 0, 0.0670152075162316, 
0, 0, 0, 0, 0, 1, 0, 0, 0), .Dim = c(6L, 24L), .Dimnames = list(
    NULL, c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", 
    "X10", "X11", "X12", "X13", "X14", "X15", "X16", "X17", "X18", 
    "X19", "X20", "X21", "X22", "X23", "X24")))

The code which I use to create a heatmap (it works for other data):

## For all of the genes
tbl_ready <- as.matrix(tbl_for[,2:25])
mode(tbl_ready)<-"numeric"

library(gplots)
heatmap.2(tbl_ready,
          Colv = NA,
          scale = "none",
          dendrogram = "row",
          col=rainbow(256, s = 1, v = 1, start = 0, end = 2/6, alpha = 1),
          density.info = "none",
          trace = "none",
          key = FALSE,
          cexCol=0.5,
          cexRow=1)

mtext(paste("ATh",""), side=3, line=-1, cex=1.5, col="black") 
mtext("Size in kDa", side=1, line=3.5, cex=1.3, col="black")  

I even tried to replace Na/NaN/Inf to 0 to avoid problems:

## Replacing Na by 0
is.na(tbl_ready) <- sapply(tbl_ready, is.infinite)
tbl_ready[is.na(tbl_ready)] <- 0
tbl_ready[is.nan(tbl_ready)] <- 0

but the error still appears:

Error in hclustfun(distfun(x)) : 
  NA/NaN/Inf in foreign function call (arg 11)
like image 344
Shaxi Liver Avatar asked May 19 '14 13:05

Shaxi Liver


3 Answers

Remove rows from your matrix that have zero variance.

like image 107
Nova Avatar answered Sep 18 '22 00:09

Nova


I too was unable to reproduce the error with the sample data provided above, but when I did

tbl_ready[1,1] <- Inf

and ran it again, I got the same error. This means there is likely an infinite value in the matrix. Are you sure that sum(is.infinite(tbl_ready)) returns 0 after your cleaning?

like image 9
MrFlick Avatar answered Nov 03 '22 21:11

MrFlick


Try d3heatmap(tbl_ready,Rowv = FALSE, Colv=FALSE)

The automatic reordering of rows and columns was causing an issue for me too, disabling it fixed the problem

like image 5
hedgedandlevered Avatar answered Nov 03 '22 21:11

hedgedandlevered