先新建个矩阵
rm(list = ls())
# Create test matrix
test = matrix(rnorm(200), 20, 10) #创建20列、10行的矩阵test
# 调整下数据
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3 #1到10行 1 3 5 7 9列数值加一
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2 #同上
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4 #同上
colnames(test) = paste("Test", 1:10, sep = "") #改变列名
rownames(test) = paste("Gene", 1:20, sep = "") #改变行名
# Draw heatmaps 画热图
library(pheatmap)
pheatmap(test) #默认参数画热图
image.png
pheatmap(test, kmeans_k = 2) #如果要在绘制热图之前聚合行,需要生成的kmeans集群的数量。如果是NA,则不聚合行。此处聚合两行
image.png
pheatmap(test, kmeans_k = 4) #我们试一下聚合四行
image.png
pheatmap(test, scale = "row", clustering_distance_rows = "correlation") #scale 设置值应该居中并按行或列方向缩放,或者不按行或列方向缩放。对应的值为“row”,“列”和“没有”。clustering_distance_rows 聚类行中使用的距离度量。可能的值是Pearson相关的“correlation”,dist支持的所有距离,如“euclidean”等。如果该值不满足上述条件,则假定提供了距离矩阵
image.png
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50)) #设置颜色
image.png
pheatmap(test, cluster_row = FALSE) #布尔值决定行是应该集群cluster还是hclust对象 默认cluster
image.png
pheatmap(test, legend = FALSE) #不画图例
image.png
# Show text within cells
pheatmap(test, display_numbers = TRUE) #显示数字
image.png
pheatmap(test, display_numbers = TRUE, number_format = "%.1e") #用指数计数法e表示
image.png
![image.png](https://img.haomeiwen.com/i15510633/04ac0a7266884e0e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
image.png
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test))) #标记值大于5的test值
image.png
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",
"1e-4", "1e-3", "1e-2", "1e-1", "1")) #不显示行cluster ;legend_breaks用于图例的断点向量
image.png
# Fix cell sizes and save to file with correct size
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap") #设置单元格宽度和高度 设置标题
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf") #图的基本字体大小为8 保存图为pdf文件
图1
# Generate annotations for rows and columns
annotation_col = data.frame(
CellType = factor(rep(c("CT1", "CT2"), 5)),
Time = 1:5
) #建立数据框 注意自动补齐
rownames(annotation_col) = paste("Test", 1:10, sep = "")
annotation_row = data.frame(
GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
) #建立数据框
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
image.png
image.png
# Display row and color annotations
pheatmap(test, annotation_col = annotation_col) #指定热图上侧显示的注释的数据帧。每一行定义特定行的特性。数据和注释中的行使用相应的列名称进行匹配。注意,配色方案考虑了变量是连续的还是离散的。
image.png
pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE) #不显示注释图例
image.png
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row) #同时显示行、列注释
image.png
# Change angle of text in the columns
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45") #列标签的角度,现在只能从几个预定义选项(0、45、90、270和315)中选择
pheatmap(test, annotation_col = annotation_col, angle_col = "0") #同上 横行显示列标签
image.png
# Specify colors
ann_colors = list(
Time = c("white", "firebrick"),
CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors, main = "Title") #改变列注释颜色
image.png
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,
annotation_colors = ann_colors) #改变列行
pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2])
image.png
image.png
# Gaps in heatmaps
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14)) #第10 row和14 row之后分割 热图
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14),
cutree_col = 2) #分割成两列 cutree_row 亦相同
image.png
image.png
# Show custom strings as row/col names 显示自定义字符串作为行/col名称
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "Il10", "Il15", "Il1b")
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row) #见图
image.png
# Specifying clustering from distance matrix 从距离矩阵指定聚类
drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
image.png
# Modify ordering of the clusters using clustering callback option 使用群集回调选项修改群集的顺序
callback = function(hc, mat){
sv = svd(t(mat))$v[,1]
dend = reorder(as.dendrogram(hc), wts = sv)
as.hclust(dend)
}
pheatmap(test, clustering_callback = callback)
image.png
复制粘贴好累....
参考来源:生信技能树
友情链接:
课程分享
生信技能树全球公益巡讲
(https://mp.weixin.qq.com/s/E9ykuIbc-2Ja9HOY0bn_6g)
B站公益74小时生信工程师教学视频合辑
(https://mp.weixin.qq.com/s/IyFK7l_WBAiUgqQi8O7Hxw)
招学徒:
(https://mp.weixin.qq.com/s/KgbilzXnFjbKKunuw7NVfw)
欢迎关注公众号:青岛生信菜鸟团
网友评论