(轉帖)-熱圖最佳實踐-pheatmap-1

原文地址:https://mp.weixin.qq.com/s/P5VcsumN8X6AfydTRY-V6Q


用pheatmap來繪圖首先要安裝這個包,它就一個功能,畫出熱圖即可,號稱是pretty heatmap,的確比其它的好用很多。我以前寫過《一步一步學習heatmap.2》的教程,很簡單的那種,所以就沒有公布在博客上面,結果發現很多其它博客居然能先我一步發出。其實包括本次的pheatmap指南,都沒什么好發,的在R里面也是傻瓜式出圖,無法就是自己熟練一下參數而已,又不是開發一個包,沒什么技術含量。我這里單獨提一下pheatmap是因為它的確非常好用,將會是我畫熱圖的不二之選。比如下面這個,是我最喜歡的:

pheatmap

里面該有的信息一應俱全了,包括基因可以分成上下調來顯色,基因和樣本都可以單獨聚類,單獨排序,樣本也可以具體再分組。熱圖也可以調整配色方案,單元格的寬度和高度都可以自由調整。把它說明書的代碼一句句運行一遍就明白了。

ftp://cran.r-project.org/pub/R/web/packages/pheatmap/pheatmap.pdf

代碼如下:

PS:我代碼復制到博客就中英文標點被弄混了,請不要直接復制我的點,一行行點敲到R里面

# Create test matrix

## Just replace the test matrix with your own data.

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 = "")

# Draw heatmaps

pheatmap(test)

pheatmap(test, kmeans_k = 2)

pheatmap(test, scale = "row", clustering_distance_rows = "correlation")

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

pheatmap(test, cluster_row = FALSE)

pheatmap(test, legend = FALSE)

# Show text within cells

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"))

# 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")

# 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 = "")

# Display row and color annotations

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)

# 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")

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])

# Gaps in heatmaps

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)

# Show custom strings as row/col names

labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "",

"", "", "Il10", "Il15", "Il1b")

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

# 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)

# 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)

## Not run:

# Same using dendsort package

library(dendsort)

callback = function(hc, ...){dendsort(hc)}

pheatmap(test, clustering_callback = callback)

## End(Not run)

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容