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()
相关推荐
兮兮能吃能睡14 小时前
R语言术语(2)
开发语言·r语言
云青黛17 小时前
肘部法找k
人工智能·算法·机器学习·聚类
Gitpchy17 小时前
Day 18 推断聚类后簇的类型
python·机器学习·聚类
小火柴12318 小时前
利用R绘制箱线图
开发语言·r语言
天桥下的卖艺者20 小时前
R语言手搓一个计算生存分析C指数(C-index)的函数算法
c语言·算法·r语言
Tiger Z1 天前
《R for Data Science (2e)》免费中文翻译 (第10章) --- Exploratory data
r语言·数据科学·中文翻译
拓端研究室1 天前
视频讲解|Python遗传算法GA在车辆路径规划VRP数据优化中的应用
开发语言·人工智能·r语言
兮兮能吃能睡2 天前
资料片:R语言中常见的英文术语及其含义
开发语言·r语言
Freya冉冉2 天前
【PYTHON学习】推断聚类后簇的类型DAY18
python·学习·聚类
绵羊20232 天前
R语言绘制热图
开发语言·r语言