第一弹 heatmap热图

先说断,理不乱

默默关注生信技能树,决心自己也要总结知识点分享出来,菜鸟一只,分享的内容有疑问请指正!

函数:画热图有众多函数,还属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 = "")
image.png

2画默认参数图

pheatmap(test)
image.png

3指定gene的聚类数

pheatmap(test, kmeans_k = 2)
image.png

4 Scale="row",按行(基因)将数scale

pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
image.png

5换个颜色

pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
image.png

6 行不聚类

 pheatmap(test, cluster_row = FALSE)
image.png

7不要图注

pheatmap(test, legend = FALSE)
image.png

8将数字标上

pheatmap(test, display_numbers = TRUE)
image.png

9 只标>5的数

pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
image.png

10 改变图注

pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",
                                                                       "1e-4", "1e-3", "1e-2", "1e-1", "1"))
image.png

11 改变每个小格子的长宽,还可以给热图加标题

pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
image.png

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 = "")
image.png

列有两个注释

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

14改变行列文本的角度

pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, angle_col = "45")
image.png
pheatmap(test, annotation_col = annotation_col, angle_col = "0")
image.png

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")
image.png
pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row, 
         annotation_colors = ann_colors)
image.png
   pheatmap(test, annotation_col = annotation_col, annotation_colors = ann_colors[2]) 
image.png

16指定行或列的gap在哪个位置

pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14))
image.png

`

pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(13, 14))
image.png
pheatmap(test, annotation_col = annotation_col, cluster_rows = FALSE, gaps_row = c(10, 14), cutree_col = 2)
image.png

17可以自定义行列的名字

labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
             "", "", "Il10", "Il15", "Il1b")

pheatmap(test, annotation_col = annotation_col, labels_row = labels_row)
image.png

18 sample-to sample distance heatmap

前面6提到了可以让行或列聚类或不聚类,要聚类的话还可以指定聚类的方法,聚类方法默认是hclust

下面是pheatmap的帮助文档中的内容:

image.png

查看hclust的帮助文档,说明hclust输入的d应该是由dist计算的

image.png

查看dist的帮助文档

image.png

因此可以自定义聚类的聚类方法,也就是dist()函数的参数

pheatmap(test, clustering_distance_rows = "minkowski", clustering_distance_cols = "minkowski")
image.png
drows = dist(test, method = "minkowski")
dcols = dist(t(test), method = "minkowski")
pheatmap(test, clustering_distance_rows = drows, clustering_distance_cols = dcols)
image.png

个人感觉上面两种方式的结果应该是一样的

当然如果你想画sample-to sample distance heatmap

sampleDists<-dist(t(test))
sampleMatrix<-as.matrix(sampleDists)
pheatmap(sampleMatrix,clustering_distance_rows=sampleDists,clustering_distance_cols = sampleDists)
image.png

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)
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容