1

The heatmap when scaling before plotting:

mat_scaled <- scale(t(mat))
pheatmap(t(mat_scaled), show_rownames=F, show_colnames=F,
         border_color=F, color=colorRampPalette(brewer.pal(6,name="PuOr"))(12))

heatmap scaling before plotting

with the scale going from [-2, 6] is completely different than when using the scaling within the pheatmap function

pheatmap(t(mat_scaled), scale="row", show_rownames=F, 
         show_colnames=F, border_color=F, color=colorRampPalette(brewer.pal(6,name="PuOr"))(12))

heatmap scaled within pheatmap function

where the scale is set from [-6,6].
Why is this difference and how could I obtain the matrix represented in the second figure?

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
Silvia
  • 11
  • 1
  • 5
  • It would be helpful if you could give a reproducible example (see here - https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-examplesee). – Graeme Frost Jun 04 '19 at 16:57

1 Answers1

3

In the second figure you plot the heatmap of the scaled matrix mat_scaled scaled a second time using the option scale="row" of pheatmap.
This is not the right way to compare external and internal scaling.
Here is the solution:

library(gridExtra)
library(pheatmap)
library(RColorBrewer)
cols <- colorRampPalette(brewer.pal(6,name="PuOr"))(12)
brks <- seq(-3,3,length.out=12)  
data(attitude)
mat <- as.matrix(attitude)

# Scale by row
mat_scaled <- t(scale(t(mat)))

p1 <- pheatmap(mat_scaled, show_rownames=F, show_colnames=F, 
         breaks=brks, border_color=F, color=cols)

p2 <- pheatmap(mat, scale="row", show_rownames=F, show_colnames=F, 
         breaks=brks, border_color=F, color=cols)

grid.arrange(grobs=list(p1$gtable, p2$gtable))

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58