使用pheatmap包
#R语言绘制热图
#生成矩阵
test = matrix(rnorm(200), 20, 10)#200个随机数,20行,10列
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
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 = "")
#绘制热图
pheatmap(test)
pheatmap(test, kmeans_k = 2)#行聚类,聚为两类
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")#scale中心化;clustering_distance_rows测量距离的方法是Person
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))#colorRampPalette设置调色板
pheatmap(test, cluster_row = FALSE)#行不聚类
pheatmap(test, legend = FALSE)#不标注图注
#显示每个方格内的标签
pheatmap(test, display_numbers = TRUE)
pheatmap(test, display_numbers = TRUE, number_format = "%.1e")#设置标签格式
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")
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")
#对行和列进行注释
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 = "")
#展示行和列的注释
pheatmap(test, annotation_row = annotation_row)#行的注释
pheatmap(test, annotation_col = annotation_col)#列的注释
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)#行列均注释
#设置行标签倾斜45度
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")
pheatmap(test, annotation_col = annotation_col, angle_col = "0")#倾斜0度
#指定颜色
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")
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])
#分割热图
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14),
cutree_col = 2)
# 自定义行/列名
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "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)
#使用集群回调选项修改集群的顺序
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)
# 也可以使用dendsort包
#install.packages("dendsort")
library(dendsort)
callback = function(hc, ...){dendsort(hc)}
pheatmap(test, clustering_callback = callback)
参考文献:
http://127.0.0.1:16053/library/pheatmap/html/pheatmap.html