user3412393
user3412393

Reputation: 105

issue with R heatmap function - generates incorrect color scheme

I've generated a simple test matrix of 0's and 1's only (just 3x3 matrix). The exact matrix I've generated is:

vector <- c(1, 0, 0, 1, 1, 1, 0, 0, 1)
matrix <- matrix(vector, nrow=3, ncol=3)
heatmap(matrix, Rowv=NA, Colv=NA)

The heatmap that is generated then comes out with white, yellow, orange, and red blocks. How can just 0's and 1's generate blocks of such different colours? Shouldn't it just be say red and white? Or yellow and white? Or something like that?

What am I doing wrong?

Upvotes: 1

Views: 1217

Answers (2)

MrFlick
MrFlick

Reputation: 206232

The default behavior for heatmap is to scale values by row (center them and divide by the standard deviation). So you're not just plotting 0's and 1's in your example. Here's what you're really plotting

m1 <- sweep(matrix, 1, rowMeans(matrix))
msd <- apply(m1, 1, sd)
m2 <- sweep(m1, 1, msd, `/`)
m2[,3:1]

#            [,1]      [,2]       [,3]
# [1,] -1.1547005 0.5773503  0.5773503
# [2,] -0.5773503 1.1547005 -0.5773503
# [3,]  0.5773503 0.5773503 -1.1547005

If you were expecting just two colors, set scale="none"

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

This is all described in the ?heatmap help page

enter image description here

Upvotes: 2

n8sty
n8sty

Reputation: 1438

This isn't a great question. I would suggest reading the ?heatmap before posting. You can easily specify colors in the heatmap function as the following demonstrates:

vector <- c(1, 0, 0, 1, 1, 1, 0, 0, 1)
matrix <- matrix(vector, nrow=3, ncol=3)
heatmap(matrix, Rowv=NA, Colv=NA,
        col = c('red', 'blue')
        )

If you want more control over the colors you can pass a function to 'col'.

Upvotes: 0

Related Questions