error arising when create heatmap in R
1
0
Entering edit mode
4.0 years ago
citronxu ▴ 20

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 • 4.0k views
ADD COMMENT
0
Entering edit mode

What are these values?

ADD REPLY
0
Entering edit mode

log2foldchange of DEGs

ADD REPLY
1
Entering edit mode
4.0 years ago
malteherold ▴ 60

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

ADD COMMENT
0
Entering edit mode

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?

ADD REPLY
0
Entering edit mode

I don't use the standard heatmap function much, but for heatmap.2 (gplots package) there is a parameter na.color. Maybe this also helps: https://sebastianraschka.com/Articles/heatmaps_in_r.html

I mostly use the pheatmap package but I am not aware that you can easily set a color for NA values with that.

The superheat package also has an option for that: https://rlbarter.github.io/superheat/missing-data.html

ADD REPLY

Login before adding your answer.

Traffic: 1479 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