2

I am following the documentation on https://jokergoo.github.io/ComplexHeatmap-reference/book/a-single-heatmap.html to create a heatmap with NAs. The documentation says that the Heatmap() function can cluster data with NA. However, I am getting an error about hclust complaining about NA in the data.

Here is my dataset: https://drive.google.com/file/d/1Wm-CUir57-eYnb0EnSxNLzKcTyLlj5Ux/view?usp=sharing

library(ComplexHeatmap)
# I did this to prepare the input matrix.
na_index = sample(c(TRUE, FALSE), nrow(in_mat)*ncol(in_mat), replace = TRUE, prob = c(1, 9))
in_mat[na_index] = NA

# And this to plot the heatmap.
ht <- Heatmap(in_mat, column_dend_side = "bottom",
              column_dend_height = unit(3, "cm"),
              column_dend_reorder = TRUE,
              column_split = 10,
              na_col = "black")

png(file.path(figures_folder, "hclust_heatmap_sig_only.png"), width = 30, height = 10, units = "in", res = 300)
draw(ht)
dev.off()

I am getting this error when I am saving the plot.

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

The error goes away if I replace NAs with zeros or switch off clustering.

I want to plot a heatmap with clustering on both rows and columns and split the columns or rows in K groups. I want to keep NAs in the data. Any suggestions would be helpful. Thanks.

Rohit Farmer
  • 319
  • 4
  • 15
  • Hi, I have encountered your same problem today. I don't have a precise answer but I have found this in the Complex Heatmap documentation, and I think it helps: "NA is allowed in the matrix. You can control the color of NA by na_col argument (by default it is grey for NA). A matrix that contains NA can be clustered by Heatmap() as long as there is no NA distances between any of the rows or columns respectively. Usually these cases correspond to sparse matrices (filled with a lot of NA values), and indicate that the unknown values should be predicted via other methods first." – s.cerioli Oct 11 '22 at 15:45

0 Answers0