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()
相关推荐
kesteler1 天前
R语言——数据类型
开发语言·r语言
WiKiLeaks_successor2 天前
R的安装以及jupyter配置windows
windows·jupyter·r语言
程序员学习随笔2 天前
StarRocks高效聚合源码解析
数据库·聚类
ALPH_2 天前
GEO数据挖掘
java·数据挖掘·数据分析·r语言·perl·r语言-4.2.1
q567315232 天前
用R语言的XML库写一个采集图片的爬虫程序
xml·爬虫·r语言
Tiger Z2 天前
R 语言科研绘图第 30 期 --- 密度图-堆叠
开发语言·程序人生·r语言·贴图
人类群星闪耀时3 天前
探索大数据分析的无限可能:R语言的应用与实践
数据挖掘·数据分析·r语言
善木科研3 天前
R语言绘图:韦恩图
数据分析·r语言·生物信息·生信分析
周周记笔记4 天前
“RStudio UI“快速指南
学习·r语言
cjy0409214 天前
面试准备——云相册项目(1)基础
linux·面试·r语言