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()
相关推荐
统计学小王子8 小时前
数学建模国赛倒计时6天——《软件工具(R语言精讲)》
开发语言·数学建模·r语言
optiz10 小时前
单细胞数据分析--多数据整合harmony聚类
数据挖掘·数据分析·聚类
statistican_ABin1 天前
全球教育水平提升预测分析报告——基于R语言随机森林回归模型的全球40国1960-2020年教育水平预测研究
随机森林·回归·r语言
gerrywhu1 天前
PostGIS实现DBSCAN空间聚类原理与应用实践【ST_ClusterDBSCAN】
聚类·postgis·dbscan·时空聚类·空间聚类分析·st_dbscan·postgis矢量数据分析
amcomputer1 天前
多聚类数据集分享-Multiple Clustering datasets
机器学习·数据挖掘·聚类
gerrywhu2 天前
PostGIS实现KMeans空间聚类原理与应用实践【ST_ClusterKMeans】
聚类·postgis·k-means聚类·clusterkmeans·矢量数据分析·空间聚类·空间 k-means
典典分享指南3 天前
用飞书多维表格搭建内容管理看板
java·python·r语言·composer·symfony
2601_962070235 天前
差异基因富集分析(R语言——GO&KEGG&GSEA)
开发语言·golang·r语言
统计学小王子6 天前
R语言入门——ggplot2图例增加公式
开发语言·r语言
统计学小王子7 天前
R语言入门——相关性热图(建模常用二)
r语言·可视化·相关性分析