This is a test version of Biostars. For the public version, visit https://www.biostars.org.
error arising when create heatmap in R

hello,

I'm dealing with DEG dataset which contains many NAs in certain treatments from RNA-seq analysis, and gonna make a heatmap in R. when I used the ComplexHeatmap package in R, following error was poping up:

Error in hclust(get_dist(t(submat), distance), method = method):NA/NaN/Inf in foreign function call (arg 10)

my dataset as below:

gene 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3L 2.64 3 -0.98 -1.79 NA -1.42 3.38 3.74 3.47 4.03 -0.89 -1.27 -0.89 0.9 1.25 NA NA
6L NA 1.79 NA NA -0.85 NA 3.15 0.99 0.73 1.87 NA NA NA NA 0.68 1.35 1.38
9L 2.59 4.44 1.43 1.53 NA NA 4.51 5.23 4.1 4.32 NA NA NA NA 1.3 NA NA

The code I used in R was simply:

mat <- read.table("path_of_txt", na.strings = "NA")
mat <- data.matrix(mat)
Heatmap(mat)

after searching around the internet for the solution from similar questions, I figure the problem may resulted from the internal function of ComplexHeatmap -hclust- being unable to calculate the distance between certain samples of my data (for example,via method which basically remove the NA value, distance of column 5 and 6 would be turned out as NA?). But I don't know what I could do to modify my dataset in order to fit the package setting or what parameters in the package could I specify to avoid this error.

appreciate any help:)

r rna-seq heatmap

What are these values?

1 answer

As you suspected, the heatmap function (you wrote Heatmap in your question) by default tries to do a clustering on the matrix, usually generating a dendrogram and reordering rows and/or columns. This does not work with missing values, hence the error message.

If you would like to use the standard heatmap function you can suppress the clustering by setting the parameters Rowv=NA and Colv=NA, you'll get more information on this if you enter ?heatmap.

mat <- read.table("path_of_txt", na.strings = "NA", header=T)
rownames(mat) = mat$gene
mat <- data.matrix(mat[-1])
heatmap(mat, Rowv=NA, Colv=NA)

fancy-heatmap

Alternatively, you could remove or replace missing values in a way that fits your question. Here removing all columns containing missing values:

mm <- mat[,!apply(mat,2, function(x) any( is.na(x))) ]
heatmap(mm)

subset-heatmap

thank you, it really works. But I'm still wondering is it possible to assign certain color, say, black, to NA plots in the heatmap?

Log in to answer this question.