Question

Functions geom_tile and scale_fill_gradient produce nice heatmaps. How to add labels to ggplot2 tiles, so that each tile has its respective value printed over it?

PS: There're many packages that do heatmaps with vairous degree of success (e.g., a survey is here: drawing heatmap with dendrogram along with sample labels). But I'm interested in ggplot2 solution, if possible.

A small code example (without labels):

library(Hmisc)
library(ggplot2)
df <- data.frame(row=sample(c("A", "B"), 10, replace=T),
                 col=sample(c("x", "y"), 10, replace=T),
                 val=rnorm(10))
sdf <- summaryBy(val~row+col, data=df, FUN=mean)
ggplot(sdf, aes(x=row, y=col)) +
  geom_tile(aes(fill = val.mean), colour = "white") +
  scale_fill_gradient(low = "white", high = "yellow")
Was it helpful?

Solution

Just:

+geom_text(aes(label=val.mean))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top