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()
相关推荐
gorgeous(๑>؂<๑)1 天前
【CVPR26-雷涛-陕西科技大学陕西省人工智能联合实验室】SPEGC:基于语义提示增强图聚类的医学图像分割持续测试时自适应
人工智能·科技·机器学习·数据挖掘·聚类
房开民2 天前
paddle 文本检测识别模型转为onnx
开发语言·r语言·paddle
全栈开发圈2 天前
干货分享|R语言聚类分析2
人工智能·机器学习·r语言
全栈开发圈3 天前
干货分享|R语言聚类分析1
开发语言·r语言
FluxMelodySun4 天前
机器学习(二十三) 密度聚类与层次聚类
人工智能·机器学习·聚类
此方ls4 天前
机器学习聚类算法二——DBSCAN(Density-Based Spatial Clustering of Applications with Noise)
算法·机器学习·聚类
放下华子我只抽RuiKe55 天前
机器学习启航:从数据直觉到模型构建的第一块基石
人工智能·深度学习·机器学习·语言模型·数据挖掘·语音识别·聚类
FluxMelodySun6 天前
机器学习(二十二) 原型聚类:k均值算法、学习向量量化、高斯混合聚类
人工智能·机器学习·聚类
AI科技星6 天前
空间光速螺旋动力学:统一质量、引力、电磁与时空本源的公理化理论与全现象验证
c语言·开发语言·opencv·算法·r语言
Σίσυφος19009 天前
PCL聚类 之区域生长
人工智能·机器学习·聚类