pheatmap热图绘制详述

# 创建测试矩阵 ----------------------------------------------------------------
library(pheatmap)  # 加载pheatmap热图绘制包

# 生成一个20行10列的随机矩阵,数据符合标准正态分布
test = matrix(rnorm(200), 20, 10)

# 对前10行的奇数列(1,3,5,7,9列)增加3个单位(制造行分组模式)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3

# 对11-20行的偶数列(2,4,6,8,10列)增加2个单位(制造列分组模式)
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2

# 对15-20行的偶列再增加4个单位(创建子分组模式)
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4

# 设置列名为Test1-Test10
colnames(test) = paste("Test", 1:10, sep = "")

# 设置行名为Gene1-Gene20
rownames(test) = paste("Gene", 1:20, sep = "")


# 绘制基础热图 ----------------------------------------------------------------
pheatmap(test)  # 绘制默认热图,包含行列聚类


# 进阶热图参数演示 ------------------------------------------------------------
# 使用k均值聚类分为2组
pheatmap(test, kmeans_k = 2)

# 按行标准化数据,使用相关性作为聚类距离
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")

# 自定义颜色梯度:从深蓝到白到火红,生成50个过渡色
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))

# 关闭行聚类(保留列聚类)
pheatmap(test, cluster_row = FALSE)

# 隐藏图例
pheatmap(test, legend = FALSE)


# 单元格内显示文本 ----------------------------------------------------------
# 显示原始数值(默认格式)
pheatmap(test, display_numbers = TRUE)

# 用科学记数法显示数字(保留1位小数),若用"%.2f"则显示两位小数
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")

# 条件显示符号:当值>5时显示*号
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))

# 自定义图例标签:将连续型图例改为分类标签显示
pheatmap(test, cluster_row = FALSE, 
         legend_breaks = -1:4, 
         legend_labels = c("0","1e-4", "1e-3", "1e-2", "1e-1", "1"))


# 单元格尺寸控制与保存 --------------------------------------------------------
# 固定单元格宽高,添加标题
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")

# 保存为PDF文件(自动调整页面尺寸),设置字体大小
pheatmap(test, cellwidth = 15, cellheight = 12, 
         fontsize = 8, filename = "test.pdf")


# 创建行列注释数据 ----------------------------------------------------------
# 列注释:包含细胞类型(CT1/CT2交替)和时间序列
annotation_col = data.frame(
  CellType = factor(rep(c("CT1", "CT2"), 5)),  # 交替重复5次
  Time = 1:5  # 时间1-5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")  # 匹配列名

# 行注释:基因分类(前10个Path1,中间4个Path2,最后6个Path3)
annotation_row = data.frame(
  GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "")  # 匹配行名


# 显示注释的热图 ------------------------------------------------------------
# 仅显示列注释
pheatmap(test, annotation_col = annotation_col)

# 隐藏注释图例
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)

# 同时显示行列注释
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)


# 调整文本角度 --------------------------------------------------------------
# 列标签旋转45度
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")

# 列标签水平显示(0度)
pheatmap(test, annotation_col = annotation_col, angle_col = "0")


# 自定义注释颜色 ------------------------------------------------------------
ann_colors = list(
  Time = c("white", "firebrick"),  # 时间梯度:白到火红
  CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),  # 指定CT1/CT2颜色
  GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")  # 基因分类颜色
)

# 应用自定义颜色并添加标题
pheatmap(test, annotation_col = annotation_col, 
         annotation_colors = ann_colors, main = "Title")

# 同时应用行列注释颜色
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, 
         annotation_colors = ann_colors)


# 添加热图间隔 --------------------------------------------------------------
# 关闭行聚类,在10和14行处添加横向间隔(根据GeneClass分组)
pheatmap(test, annotation_col = annotation_col, 
         cluster_rows = FALSE, gaps_row = c(10, 14))

# 同时横向切割列为2组
pheatmap(test, annotation_col = annotation_col, 
         cluster_rows = FALSE, gaps_row = c(10, 14), 
         cutree_col = 2)


# 自定义行列标签 ------------------------------------------------------------
# 仅显示最后三个基因名称(其他为空)
labels_row = c(rep("", 17), "Il10", "Il15", "Il1b")

pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)


# 自定义距离算法 ------------------------------------------------------------
# 使用闵可夫斯基距离计算行列距离矩阵
drows = dist(test, method = "minkowski")    # 行距离
dcols = dist(t(test), method = "minkowski") # 列距离(需要转置矩阵)

# 应用自定义距离矩阵进行聚类
pheatmap(test, 
         clustering_distance_rows = drows,  # 行聚类距离
         clustering_distance_cols = dcols)  # 列聚类距离
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值