This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Pheatmap and SD

Hello everyone,

I have to generate a Heatmap using Pheatmap package in R

The heatmap as to be scaled by row with mean = 0 and SD = 1.

Now my command line is this:

pheatmap(data,scale = "row")

I can tell that scaling by row makes a good difference but how can I set a mean = 0 and SD = 1 ?

Thanks for help

rna-seq

1 answer

Hey Morris,

You are doing it correctly as is. The pheatmap row-scaling function is the following:

pheatmap.scale <- function(x) {
  m = apply(x, 1, mean, na.rm = T)
  s = apply(x, 1, sd, na.rm = T)
  return((x - m) / s)
}

Let's create random data:

randomdata <- matrix(rexp(2000000, rate=.1), ncol=2000)

Now let's scale with pheatmap()'s row-scaling function:

randomdata.scaled <- pheatmap.scale(randomdata)

Now check the mean and sd of each row:

all(round(apply(randomdata.scaled, 1, mean), 3) == 0)
[1] TRUE

all(round(apply(randomdata.scaled, 1, sd), 3) == 1)
[1] TRUE

---------------------------

Note that this is exactly the same as scaling outside of pheatmap(), like this:

randomdata.scaled <- t(scale(t(randomdata)))

all(round(apply(randomdata.scaled, 1, mean), 3) == 0)
[1] TRUE

all(round(apply(randomdata.scaled, 1, sd), 3) == 1)
[1] TRUE

------------------------------

So —don't worry— you are doing it correctly.

Kevin

Log in to answer this question.