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()
相关推荐
Xyz_Overlord21 小时前
机器学习——聚类算法
算法·机器学习·聚类
善木科研1 天前
读文献先读图:GO弦图怎么看?
机器学习·数据分析·r语言
Tiger Z1 天前
R 语言科研绘图第 55 期 --- 网络图-聚类
开发语言·r语言·贴图
十三画者1 天前
【数据分析】R版IntelliGenes用于生物标志物发现的可解释机器学习
python·机器学习·数据挖掘·数据分析·r语言·数据可视化
lishaoan774 天前
实验设计与分析(第6版,Montgomery著,傅珏生译) 第10章拟合回归模型10.9节思考题10.12 R语言解题
回归·r语言·线性回归·残差分析·实验设计与数据分析·回归显著性
南瓜胖胖4 天前
【R语言编程绘图-mlbench】
开发语言·机器学习·r语言
天桥下的卖艺者4 天前
R语言使用随机过采样(Random Oversampling)平衡数据集
开发语言·r语言
白熊1885 天前
【机器学习基础】机器学习入门核心算法:层次聚类算法(AGNES算法和 DIANA算法)
算法·机器学习·聚类
Biomamba生信基地5 天前
R语言基础| 创建数据集
开发语言·r语言·生信·医药
lishaoan775 天前
实验设计与分析(第6版,Montgomery)第5章析因设计引导5.7节思考题5.19 R语言解题
r语言·方差分析·实验设计与分析·残差分析·正态假设·交互作用