Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put labels on tiles in ggplot2 in R?

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")
like image 299
Anton Tarasenko Avatar asked Dec 11 '13 05:12

Anton Tarasenko


1 Answers

Just:

+geom_text(aes(label=val.mean))
like image 116
Troy Avatar answered Oct 18 '22 20:10

Troy