R绘图 | pheatmap 聚类并设置间隔

一个pheatmap的小例子,包含设置列注释,列注释颜色,聚类,聚类间隔,保存为pdf等。

  • 加方框失败,可以后续使用Illustrator手工添加

效果图:

复制代码
library(pheatmap)
set.seed(2025)
mat <- matrix(rnorm(100), nrow=20)
rownames(mat)=paste0("cell", 1:nrow(mat))
# 相关系数
mat.cor=cor( t(mat), method="spearman")
colnames(mat.cor)=rownames(mat)

# 列注释: 区分前10个ctrl组blue,后10个细胞treat组red
annotation_col = data.frame(
  id=colnames(mat.cor),
  group=c( rep("ctrl", 10), rep("treat", 10) ), 
  row.names=1
)
# 设置颜色
ann_colors = list(
  group = c(ctrl = "navy", treat = "deeppink")
)

# 热图
library(grid)
#ComplexHeatmap::pheatmap(mat.cor,
p1=pheatmap::pheatmap(mat.cor,
         #border_color = "white",
         border_color = NA,
         # 加方框:失败
         #add_geom = "rectangles",
         #rect_gp = gpar(fill = "transparent", col = "red", lwd = 3),
         #rect_row = c(1, 5), rect_col = c(2, 7),
         show_rownames = TRUE,
         #gaps_row = c(1,3,7, 10), #适用于非聚类时
         cutree_row=5, cutree_cols=5, #适用于聚类时
         clustering_method = "ward.D2",
         annotation_col = annotation_col, #annotation_row = annotation_row,
         annotation_colors = ann_colors,
         #color = c(colorRampPalette(colors = c("white","yellow"))(20),colorRampPalette(colors = c("yellow","firebrick3"))(20)),
         show_colnames = TRUE, main="cutree demo")
#保存pdf
pdf(paste0("D://other//demo.heatmap.pdf"), width=6, height=5)
grid::grid.newpage()
grid::grid.draw(p1$gtable)
dev.off()
相关推荐
高-老师3 小时前
基于R语言的贝叶斯网络模型的实践技术应用;R语言实现Bayesian Network分析的基本流程
开发语言·r语言·贝叶斯网络
AC赳赳老秦19 小时前
科研数据叙事:DeepSeek将实验数据转化为故事化分析框架
开发语言·人工智能·数据分析·r语言·时序数据库·big data·deepseek
Testopia1 天前
AI编程实例 - 爆款文章预测:K-Means聚类与分类算法的实践
人工智能·分类·kmeans·ai编程·聚类
Kratzdisteln1 天前
【MCM】((N_G,N_A)) 可行域; ((C_R,c_E)) 分区图
c语言·开发语言·r语言
张小凡vip1 天前
数据挖掘(八)--让Jupyter notebook支持R语言环境
jupyter·数据挖掘·r语言
青灯照颦微2 天前
【R】三种方式安装R包
开发语言·r语言
weixin_462446233 天前
PaddleX 3.2 人脸识别实战:自定义人脸库 + CartoonFace 官方案例 Top-K 识别完整指南
开发语言·r语言
Tiger Z3 天前
《R for Data Science (2e)》免费中文翻译 (第19章) --- Joins(1)
r语言·编程·数据科学
Tiger Z3 天前
《R for Data Science (2e)》免费中文翻译 (第18章) --- Missing values
开发语言·r语言