3

I am creating heatmaps using the pheatmap() function using the following code:

library(pheatmap)
pheatmap((data_matrix[,1:11]), cluster_rows = F, cluster_cols = F, scale="none",
     show_rownames=F, treeheight_row = F, 
     color=colorRampPalette(rev(c("red","white")))(50),
     main="values", cex.main=1.2)

I would like to add text and lines at some positions to indicate groups and percentages.

abline() and text() don't work and with overplotting I was not successful so far.

What is the best way to do that?

Thanks, Philipp

nico
  • 50,859
  • 17
  • 87
  • 112
user3485328
  • 41
  • 1
  • 4
  • Very strange that `text()` and `abline()` don't work for you there... Maybe you should check the system of coordinates in use by `pheatmap`, just in case you would be plotting your text and lines out of the graphic frame... – jaybee Aug 11 '14 at 11:01

2 Answers2

8

If you look at the source code for the pheatmap function you will see that, after some fiddling with the data it calls the heatmap_motor function.

Looking at this function (it is not exported, so you will have to type pheatmap:::heatmap_motor to see its source code) you will see that it uses functions from the grid package to plot the heatmap, hence using abline or text won't work, but grid.abline or grid.text will!

For instance (note that the coordinates were found by a trial-and-error process):

library(pheatmap)
library(grid)

a <- matrix(rnorm(100), 10, 10)
pheatmap(a, cluster_rows = F, cluster_cols = F)

grid.text(1:100, x=rep(seq(0.05, 0.91, length.out=10), 10), 
          y=rep(seq(0, 1, 0.1)+0.05, each=10))
nico
  • 50,859
  • 17
  • 87
  • 112
  • thanks for the explanation and the hint to use `grid.abline` and `grid.text` instead. In the end I used `grid.text` and `grid.lines` as I could not limit the length of `grid.abline`. – user3485328 Aug 11 '14 at 12:50
5

You can use display_numbers option in pheatmap. For example,

library(pheatmap)

a <- matrix(rnorm(90), 9, 10)
txt <- matrix(sample(letters, replace = TRUE, 90), 9, 10)
pheatmap(a, cluster_rows = F, cluster_cols = F, display_numbers = txt)

The result figure: pheatmap with texts

zhanxw
  • 3,159
  • 3
  • 34
  • 32