1. ggplot2 heatmap
library(ggplot2)
dat <- data.frame(
row = c("A", "A", "B", "B", "C", "C"),
col = c("X", "Y", "X", "Y", "X", "Y"),
value = c(1, 2, 3, 4, 5, 6)
)
head(dat)
# 基础版热图
ggplot(dat, aes(x = col, y = row, fill = value)) +
geom_tile()
# 自定义颜色映射范围:scale_fill_gradient()
ggplot(dat, aes(x = col, y = row, fill = value)) +
geom_tile() +
scale_fill_gradient(low = "blue", high = "red")
# labs()函数进行设置
ggplot(dat, aes(x = col, y = row, fill = value)) +
geom_tile() +
scale_fill_gradient(name="Score", low = "blue", high = "red") +
labs(title = "Heatmap", x = "Column", y = "Row")+
theme_minimal()
2. 热图元素的拼接
一个完整的热图包括:
- 由许多小方格组成的颜色模块,不同的颜色代表不同的数据
- 分组条形图,代表数据的分组
- 聚类树,代表数据的聚集情况
✅小方格可以用ggplot2中的geom_tile()函数来画
✅分组条形图也是一样
✅聚类树可以用ggtree()函数来画
👏🏻然后我这里使用了aplot包将上述三个元素拼到一起,形成了完整的热图
- https://mbd.baidu.com/newspage/data/dtlandingsuper?nid=dt_3787382589870285191
- https://blog.csdn.net/weixin_49320263/article/details/132280176