creating a heatmap where the data has NaN values in it

Viewed 31337

I am trying to create a heatmap using the heatmap.2 package. My data has lot's of NaN values in it, and what I would like to do is the following. Every time there is a NaN value, simply have the cell be colored as light grey (or some other neutral color, maybe white), and all of the other values (which are log2 expression) to have a standard green/yellow/red coloring scheme. Here is my code that I have ben using:

heatmap.2(as.matrix(foo2[rowSums (abs(foo2)) != 0,]),
          col = redgreen,
          margins = c(12, 22),
          trace = "none", 
          xlab = "Comparison",
          lhei = c(2, 8),
          scale = c("none"),
          symbreaks = min(foo2 = 0, na.rm = TRUE),
          na.color = "blue",
          cexRow = 0.5,
          cexCol = .7,
          main = "DE geness",
          Colv = F)

This works well when there is no NaN values, but when the data has NaN, I am getting an error which says:

Error in hclustfun(distfun(x)) : 
  NA/NaN/Inf in foreign function call (arg 11)

Essentially, I would like to have it ignore the NaN's in the data. I am not sure how to handle this. any help would be greatly appreciated.

5 Answers

Just a suggestion for a practical solution in addition to posdef's very instructive answer:

Since distfun is only used to determine the structure of the dendrogram, you can simply replace the NA's in the dist matrix with values that are a bit higher than the maximum of the non-NA values.

For this, we need a new distance function (one that wraps the normal dist function and just replaces NAs):

dist_no_na <- function(mat) {
    edist <- dist(mat)
    edist[which(is.na(edist))] <- max(edist, na.rm=TRUE) * 1.1 
    return(edist)
}

and make use of this function in the heatmap.2 call:

heatmap.2(mat, ..., dendrogram="row", Colv="NA", na.color="black", distfun=dist_no_na)

Properties

This is of course not a perfect solution. It assigns numerical distance values to pairs of vectors for which there is no basis on which a (euclidean?) distance can be computed. However, it does have some desirable properties.

  1. The heatmap.2 function works :-)

  2. Rows that only contain NA's for instance are then split from the main branch first (which reflects the issue at hand nicely).

  3. I am not entirely certain which effect it has to replace NA values that are caused by other properties of the matrix. posdef pointed out that there may be such NA values. In posdef's example, there are two rows for which there is no pair of non-NA entries in the same column - i.e. it is impossible to determine a euclidean distance. It is in this case, probably still be appropriate to reflect this as a particularly large distance larger than all those that can be computed numerically.

I would not choose a replacement value much larger than the non-NA maximum. (The chosen value in the code above is 10% larger.) This would increase the distance of the split-off point of all-NA rows to the following split-off points (the relevant part of the dendrogram) and may make the relevant part of the dendrogram difficult to see.

I apologise if this seems like I am over simplifying it but I know I would appreciate a simplified post like this (since I am no expert in R). I found this the easiest method so far and I'll show it with my data;

My data ranges from 0 to 114 in a data matrix with a lot of NA values so what I did was first replace all NA values with -1 (below the range of my dataset)

x <- mymatrix %>% replace(is.na(.), -1)

then I set breaks using heatmap.2(). If you want your NA values to be let's say "black" and the rest of the values to use a colourpalette with a range of colours then set your breaks using seq(). Since my data ranges from 0 to 114, I set my seq to go from 0 to 114 by increments of 1. Then using heatmap.2() I set the breaks as -1 and then my sequence (so the breaks would look like (-1,0,1,2,3..etc). I set the colours to be "black" for the -1 values (the NAs) and use 114 colours from the bluered palette for the remaining values.

seq <- seq(from = 0, to = 114, by = 1)
heatmap.2(x, col = c("black", bluered(114)), 
      trace = "none", density.info = "none", breaks=c(-1,seq))

I hope this is helpful!

Related