2

I just noticed that the plots from using heatmap() function and image() function look different even though I'm using the same data matrix. I have the following code:

set.seed(12345)
dataMatrix <- matrix(rnorm(400), nrow =40)

set.seed(678910)
for(i in 1:40) {
# flip a coin
coinFlip <- rbinom(1, size =1, prob =0.5)
# if coin is heads add a common pattern to that row
if(coinFlip) {
dataMatrix[i, ] <- dataMatrix[i, ] + rep(c(0,3), each =5)
}}

hh <- hclust(dist(dataMatrix))
dataMatrixOrdered <- dataMatrix[hh$order, ]
image(t(dataMatrixOrdered)[, nrow(dataMatrixOrdered):1])
heatmap(t(dataMatrixOrdered)[, nrow(dataMatrixOrdered):1])

As far as I have understood, the only difference between the 2 functions is that, the heatmap() function does a cluster analysis to produce dendrograms for rows and columns. Hence the plots should look the same. Can someone please tell me why I see this difference? I have attached the plots from the 2 functions.Plot using heatmapPlot using image

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
user3922546
  • 187
  • 1
  • 6
  • 16
  • 2
    heatmap also reorders rows & columns by default; see the help page – Ben Bolker Jan 31 '15 at 18:48
  • Please tell us what the diff is. As BenB points out , different plotting routines orient the axes differently. Is this your concern, or do you have a spatial resolution issue, or a contrast issue, etc.? – Carl Witthoft Jan 31 '15 at 19:06
  • I tried using the arguments Rowv =NA, Colv=NA which the R help page says that no reordering would be done but I think only the dendrograms are suppressed but ordering still takes place it seems. – user3922546 Jan 31 '15 at 19:56

1 Answers1

0

Indeed the two functions produce consistently different patterns. I have no idea how that happens, but this happens whether you reorder the data or not.

Just to facilitate visualizing the differences, I turned dataMatrix into a distance matrix. The heatmap displaying this distance matrix should be fully symetrical. Image does give a symetric heatmap, heatmap does not.

set.seed(12345)
dataMatrix <- matrix(rnorm(400), nrow =40)

set.seed(678910)
for(i in 1:40) {
# flip a coin
coinFlip <- rbinom(1, size =1, prob =0.5)
# if coin is heads add a common pattern to that row
if(coinFlip) {
dataMatrix[i, ] <- dataMatrix[i, ] + rep(c(0,3), each =5)
}}

dataMatrix <- as.matrix(dist(dataMatrix))

image(dataMatrix)

heatmap(dataMatrix, Rowv = NA, Colv = NA)

What happens is that heatmap is scaling the rows before doing the coloring. If you avoid this scaling, the two functions give the same heatmap.

heatmap(dataMatrix, Rowv = NA, Colv = NA, scale = "none")

You can read more about this here