如何在R的ggplot2中的瓷砖上贴标签?

时间:2021-07-27 13:46:45

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?

函数geom_tile和scale_fill_gradient生成很好的热图。如何在ggplot2图块中添加标签,以便每个图块都打印出相应的值?

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.

PS:有许多软件包可以做出成功的热图(例如,调查在这里:使用树状图和样本标签绘制热图)。但是如果可能的话,我对ggplot2解决方案很感兴趣。

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")

1 个解决方案

#1


11  

Just:

+geom_text(aes(label=val.mean))

#1


11  

Just:

+geom_text(aes(label=val.mean))