先说断,理不乱
默默关注生信技能树,决心自己也要总结知识点分享出来,菜鸟一只,分享的内容有疑问请指正!
函数:画热图有众多函数,还属pheatmap好
1test data
library("pheatmap")
# Create test matrix
test = matrix(rnorm(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 = "")
2画默认参数图
pheatmap(test)
3指定gene的聚类数
pheatmap(test, kmeans_k = 2)
4 Scale="row",按行(基因)将数scale
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
5换个颜色
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
6 行不聚类
pheatmap(test, cluster_row = FALSE)
7不要图注
pheatmap(test, legend = FALSE)
8将数字标上
pheatmap(test, display_numbers = TRUE)
9 只标>5的数
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
10 改变图注
pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",
"1e-4", "1e-3", "1e-2", "1e-1", "1"))
11 改变每个小格子的长宽,还可以给热图加标题
pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
12 filename存图片,不直接显示
pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")
dev.off()
dev.new()
13给行列加注释
##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 = "")
列有两个注释
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)
14改变行列文本的角度
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")
pheatmap(test, annotation_col = annotation_col, angle_col = "0")
15改变注释的颜色
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])
16指定行或列的gap在哪个位置
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(13, 14))
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), cutree_col = 2)
17可以自定义行列的名字
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "Il10", "Il15", "Il1b")
pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
18 sample-to sample distance heatmap
前面6提到了可以让行或列聚类或不聚类,要聚类的话还可以指定聚类的方法,聚类方法默认是hclust
下面是pheatmap的帮助文档中的内容:
查看hclust的帮助文档,说明hclust输入的d应该是由dist计算的
查看dist的帮助文档
因此可以自定义聚类的聚类方法,也就是dist()函数的参数
pheatmap(test, clustering_distance_rows = "minkowski", clustering_distance_cols = "minkowski")
drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
个人感觉上面两种方式的结果应该是一样的
当然如果你想画sample-to sample distance heatmap
sampleDists<-dist(t(test))
sampleMatrix<-as.matrix(sampleDists)
pheatmap(sampleMatrix,clustering_distance_rows=sampleDists,clustering_distance_cols = sampleDists)
19 修改聚类的顺序
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)