PS:我只是個(gè)搬運(yùn)工
文獻(xiàn)中的熱圖的解讀:
http://www.360doc.com/content/18/0316/15/51841481_737529918.shtml
熱圖是對實(shí)驗(yàn)數(shù)據(jù)分布情況進(jìn)行分析的直觀可視化方法,可以用來進(jìn)行實(shí)驗(yàn)數(shù)據(jù)的質(zhì)量控制和差異數(shù)據(jù)的具像化展示荤牍,還可以對數(shù)據(jù)和樣品進(jìn)行聚類康吵,觀測樣品質(zhì)量鞋既。
它有多種形式邑闺,但基本的元素卻是通用的陡舅。例如下圖灾炭,就是一副組學(xué)研究中熱圖的常用繪制模式蜈出,每個(gè)小方格表示每個(gè)基因铡原,其顏色表示該基因表達(dá)量大小,表達(dá)量越大顏色越深(紅色為上調(diào)只泼,綠色為下調(diào))请唱。每行表示每個(gè)基因在不同樣本中的表達(dá)量情況,每列表示每個(gè)樣品中所有基因的表達(dá)量情況孽惰。上方樹形圖表示對來自不同實(shí)驗(yàn)分組的不同樣品的聚類分析結(jié)果勋功,左側(cè)樹狀圖表示對來自不同樣本的不同基因的聚類分析結(jié)果:
在文章中,熱圖通常有兩大作用:數(shù)據(jù)質(zhì)量控制和直觀展示重點(diǎn)研究對象的差異變化情況骚揍。
首先信不,我們來說說熱圖在數(shù)據(jù)質(zhì)量控制(質(zhì)控)過程中的應(yīng)用。如下圖下硕,通過觀察上方樹形圖(對列的聚類分析)大家可以明顯的看到梭姓,兩個(gè)實(shí)驗(yàn)分組中的Experiment組,其基因的總體表達(dá)模式與作為對照的control組存在很大的差異:在對照組中释牺,多數(shù)基因都呈現(xiàn)極大下調(diào)的表達(dá)模式(綠色條帶)没咙,而實(shí)驗(yàn)組則正好相反牌捷,多數(shù)基因?yàn)樯险{(diào)模式暗甥。屬于對照組的三個(gè)樣本(C1棒口、C4和C5)的表達(dá)模式相似,屬于實(shí)驗(yàn)組的三個(gè)樣本(E1漾肮、E4和E5)的表達(dá)模式相似。這證明無論是對照組還是實(shí)驗(yàn)組,各自的樣本重復(fù)性都是很好的夜只,至少可以證明其在實(shí)驗(yàn)處理上是不存在大的失誤的蒜魄,得出的數(shù)據(jù)也是可信的、可靠的伞鲫、符合邏輯的。而一旦出現(xiàn)同一組的某一個(gè)或某幾個(gè)樣本的表達(dá)模式與本組內(nèi)其他樣本表達(dá)模式迥異的情況签舞,則需要小心調(diào)查前期實(shí)驗(yàn)是否存在問題了秕脓。
熱圖在文章中的另一大作用就是直觀展示重點(diǎn)研究對象的表達(dá)量數(shù)據(jù)差異變化情況柒瓣。通過上文的介紹,大家可能已經(jīng)發(fā)現(xiàn)了一個(gè)問題吠架,那就是一次實(shí)驗(yàn)中檢測到的基因或蛋白往往成干上萬芙贫,導(dǎo)致一副全局性的熱圖的行數(shù)(基因或蛋白數(shù))也相應(yīng)地十分龐大,使得在一副圖片大小的篇幅內(nèi)傍药,代表單個(gè)基因或蛋白的每一個(gè)小方格的信息(如基因名和表達(dá)量等)幾乎不可能被肉眼所識別。所以類似的用整個(gè)數(shù)據(jù)集畫出的熱圖往往只能用于數(shù)據(jù)的整體質(zhì)控。而要能夠向讀者清晰展示自己所研究的某一批基因或蛋白的數(shù)據(jù)分布與變化情況,就應(yīng)當(dāng)如下圖所示(原文鏈接http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3759040/),把自己的研究對象從數(shù)據(jù)集中找出來再繪制熱圖(作為重點(diǎn)關(guān)注的幾個(gè)標(biāo)志性的基因在右側(cè)以黑色標(biāo)注):
那么熱圖應(yīng)該如何繪制呢?
首先查看R當(dāng)中的heatmap說明書:
格式:
heatmap(x, Rowv = NULL, Colv =if(symm)"Rowv" else NULL,
??????? distfun = dist, hclustfun =hclust,
??????? reorderfun = function(d, w)reorder(d, w),
??????? add.expr, symm = FALSE, revC =identical(Colv, "Rowv"),
??????? scale = c("row","column", "none"), na.rm = TRUE,
??????? margins = c(5, 5), ColSideColors,RowSideColors,
??????? cexRow = 0.2 + 1/log10(nr),cexCol = 0.2 + 1/log10(nc),
??????? labRow = NULL, labCol = NULL,main = NULL,
??????? xlab = NULL, ylab = NULL,
??????? keep.dendro = FALSE, verbose =getOption("verbose"), ...)
參數(shù):
x
numeric matrix of
the values to be plotted.要繪制的值的數(shù)字矩陣亲族。
Rowv
determines if and
how the row dendrogram should be computed and reordered. Either a dendrogram or
a vector of values used to reorder the row dendrogram or NA to suppress any row
dendrogram (and reordering) or by default, NULL, see ‘Details’ below.確定是否以及如何計(jì)算和重新排序行樹狀圖。樹狀圖或用于對行樹狀圖進(jìn)行重新排序的值向量戈次,或用于抑制任何行樹狀圖(和重新排序)的NA,默認(rèn)情況下為NULL懂缕,請參閱下面的“詳細(xì)信息”工碾。
Colv
determines if and
how the column dendrogram should be reordered. Has the same options as the Rowv
argument above and additionally when x is a square matrix, Colv =
"Rowv" means that columns should be treated identically to the rows
(and so if there is to be no row dendrogram there will not be a column one
either).確定是否以及如何對列樹狀圖重新排序火惊。與上面的Rowv參數(shù)具有相同的選項(xiàng)惶岭,此外嗜浮,當(dāng)x是方陣時(shí)辞居,Colv = "Rowv"意味著列應(yīng)該與行相同(因此,如果沒有行樹狀圖禁添,也不會有列樹狀圖)。
distfun
function used to
compute the distance (dissimilarity) between both rows and columns. Defaults to
dist.函數(shù),用于計(jì)算行和列之間的距離(差異)呢袱。默認(rèn)為dist遭顶。
hclustfun
function used to
compute the hierarchical clustering when Rowv or Colv are not dendrograms.
Defaults to hclust. Should take as argument a result of distfun and return an
object to which as.dendrogram can be applied.當(dāng)Rowv或Colv不是樹狀圖時(shí),用于計(jì)算層次聚類的函數(shù)橘券。默認(rèn)為hclust。應(yīng)該以distfun的結(jié)果作為參數(shù),并返回一個(gè)可應(yīng)用as.dendrogram的對象。
reorderfun
function(d, w) of
dendrogram and weights for reordering the row and column dendrograms. The
default uses reorder.dendrogram.樹狀圖的函數(shù)(d, w)和用于對行属百、列樹狀圖重新排序的權(quán)重爷辱。默認(rèn)使用reorder.dendrogram刘急。
add.expr
expression that
will be evaluated after the call to image. Can be used to add components to the
plot.在調(diào)用image之后計(jì)算的表達(dá)式浪谴。可用于向繪圖添加組件。
symm
logical indicating
if x should be treated symmetrically; can only be true when x is a square
matrix.邏輯指示x是否應(yīng)被對稱對待;只有當(dāng)x是方陣時(shí)才成立员凝。
revC
logical indicating
if the column order should be reversed for plotting, such that e.g., for the
symmetric case, the symmetry axis is as usual.邏輯指示在繪圖時(shí)列的順序是否應(yīng)該顛倒窃这,例如杭攻,在對稱情況下辣垒,對稱軸是正常的铜邮。
scale
character indicating
if the values should be centered and scaled in either the row direction or the
column direction, or none. The default is "row" if symm false, and
"none" otherwise.字符玖瘸,指示值是否應(yīng)居中并按行或列方向縮放,或不按行或列縮放檀咙。如果symm為false雅倒,默認(rèn)為“row”,否則為“none”弧可。
na.rm
logical indicating
whether NA's should be removed.邏輯指示是否應(yīng)該移除NA蔑匣。
margins
numeric vector of
length 2 containing the margins (see par(mar = *)) for column and row names,
respectively.長度為2的數(shù)字向量,分別包含列和行名稱的邊距(參見par(mar = *))棕诵。
ColSideColors
(optional)
character vector of length ncol(x) containing the color names for a horizontal
side bar that may be used to annotate the columns of x.(可選)長度ncol(x)的字符向量裁良,包含水平側(cè)欄的顏色名稱,可用于標(biāo)注x的列校套。
RowSideColors
(optional)
character vector of length nrow(x) containing the color names for a vertical
side bar that may be used to annotate the rows of x.(可選)長度為nrow(x)的字符向量,包含用于注釋x行的垂直側(cè)欄的顏色名稱搔确。
cexRow, cexCol
positive numbers,
used as cex.axis in for the row or column axis labeling. The defaults currently
only use number of rows or columns, respectively.正數(shù)灭忠,用cex表示座硕。軸在用于行或列軸標(biāo)記弛作。目前华匾,默認(rèn)值僅分別使用行數(shù)或列數(shù)。
labRow, labCol
character vectors
with row and column labels to use; these default to rownames(x) or colnames(x),
respectively.要使用的帶有行和列標(biāo)簽的字符向量;它們分別默認(rèn)為rownames(x)或colnames(x)蜘拉。
main, xlab, ylab
main, x- and
y-axis titles; defaults to none.主萨西、x軸和y軸標(biāo)題;默認(rèn)為沒有。
keep.dendro
logical indicating
if the dendrogram(s) should be kept as part of the result (when Rowv and/or
Colv are not NA).邏輯指示樹狀圖是否應(yīng)該作為結(jié)果的一部分(當(dāng)Rowv和/或Colv不是NA時(shí))旭旭。
verbose
logical indicating
if information should be printed.
邏輯指示是否應(yīng)該打印信息持寄。
其他基礎(chǔ)設(shè)置
main?圖的名字
file?要保存圖的名字
color?表示顏色,賦值漸變顏色調(diào)色板colorRampPalette屬性废麻,選擇“綠模庐,黑,紅”漸變掂碱,分為100個(gè)等級,顶吮,例:color = colorRampPalette(c(“navy”, “white”, “firebrick3”))(102)
sclae?表示值均一化的方向,或者按照行或列搏恤,或者沒有湃交,值可以是"row", “column” 或者"none"
margins?表示頁邊空白的大小
fointsize?表示每一行的字體大小
聚類相關(guān)設(shè)置
cluster_cols?表示進(jìn)行列的聚類,值可以是FALSE或TRUE
cluster_row?同上息罗,是否進(jìn)行行的聚類
treeheight_row?設(shè)置row方向的聚類樹高
treeheight_col?設(shè)置col方向的聚類樹高
clustering_distance_row?表示行距離度量的方法
clustering_distance_cols?同上才沧,表示列距離度量的方法
clustering_method?表示聚類方法绍刮,值可以是hclust的任何一種,如"ward.D",“single”, “complete”(默認(rèn)), “average”, “mcquitty”, “median”, “centroid”, “ward.D2”
legend設(shè)置
legend?TRUE或者FALSE孩革,表示是否顯示圖例
legend_breaks?設(shè)置圖例的斷點(diǎn)得运,格式:vector
legend_labels?legend_breaks對應(yīng)的標(biāo)簽 例:legend_breaks = -1:4, legend_labels = c(“0”,“1e-4”, “1e-3”, “1e-2”, “1e-1”, “1”)
單元格設(shè)置
border_color?表示熱圖上單元格邊框的顏色,如果不繪制邊框饱搏,則使用NA
cellheight?表示每個(gè)單元格的高度
cellwidth?表示每個(gè)單元格的寬度
單元格中的數(shù)值顯示:
display_numbers?表示是否將數(shù)值顯示在熱圖的格子中置逻,如果這是一個(gè)矩陣(與原始矩陣具有相同的尺寸),則顯示矩陣的內(nèi)容而不是原始值坤学。
fontsize?表示熱圖中字體顯示的大小
number_format?設(shè)置顯示數(shù)值的格式报慕,較常用的有"%.2f"(保留小數(shù)點(diǎn)后兩位)眠冈,"%.1e"(科學(xué)計(jì)數(shù)法顯示菌瘫,保留小數(shù)點(diǎn)后一位)
number_color?設(shè)置顯示內(nèi)容的顏色
熱圖分割設(shè)置
cutree_rows?基于層次聚類(使用cutree)劃分行的簇?cái)?shù)(如果未聚集行,則忽略參數(shù))
cutree_cols?基于層次聚類(使用cutree)劃分列的簇?cái)?shù)
annotation相關(guān)設(shè)置
annotation_row?行的分組信息雇盖,需要使用相應(yīng)的行名稱來匹配數(shù)據(jù)和注釋中的行崔挖,注意之后顏色設(shè)置會考慮離散值還是連續(xù)值庵寞,格式要求為數(shù)據(jù)框
annotation_col?同上,列的分組信息
annotation_colors?用于手動(dòng)指定annotation_row和annotation_col track顏色的列表脓鹃。
annotation_names_row?boolean值瘸右,顯示是否應(yīng)繪制行注釋track的名稱。
[if !supportLists]·???????[endif]annotation_names_col?同上逞频,顯示是否應(yīng)繪制列注釋track的名稱
下面嘗試進(jìn)行繪制:
# 測試數(shù)據(jù)
a=c(12,14,17,11,16)
b=c(4,20,15,11,9)
c=c(5,7,19,8,18)
d=c(15,13,11,17,16)
e=c(12,19,16,7,9)
A=cbind(a,b,c,d,e)
B=rbind(a,b,c,d,e)
require(graphics); require(grDevices)
x? <-as.matrix(A)
rc <- rainbow(nrow(x), start = 0, end = .3)
cc <- rainbow(ncol(x), start = 0, end = .3)
hv <- heatmap(x, col = cm.colors(256), scale ="column",
????????????? RowSideColors= rc, ColSideColors = cc, margins = c(5,10),
?????????????xlab = "specification variables", ylab =? "Car Models",
?????????????main = "heatmap(, ..., scale =\"column\")")
utils::str(hv)
關(guān)于其他設(shè)置還需要再熟悉一遍
下面跟隨網(wǎng)友的帖子跑一遍過程
http://www.reibang.com/p/718b22bb1148
1. 安裝pheatmap包
install.packages("pheatmap")
library(pheatmap)
?
2. 創(chuàng)建測試矩陣
test = matrix(rnorm(200), 20, 10) #test為一個(gè)20*10的矩陣苗胀,200個(gè)元素滿足參數(shù)為0和1的正態(tài)分布
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 = "") #
定義列名基协,注意paste的用法
rownames(test) = paste("Gene", 1:20, sep = "") #
定義行名
rownames(test) = paste("Gene", 1:20, sep = "") #定義行名
?
3. 畫個(gè)熱圖
3.1 pheatmap(test)
基本用法菇用,根據(jù)“2. 創(chuàng)建測試矩陣”中的局部賦值運(yùn)算可以得到明顯的分區(qū)。
?
3.2 pheatmap(test, kmeans_k = 3)
將行聚為幾類
kmeans是一種聚類算法杂穷,詳見https://www.cnblogs.com/bourneli/p/3645049.html
3.3 pheatmap(test, scale = "row")
標(biāo)準(zhǔn)化
為什么要標(biāo)準(zhǔn)化耐量? 原始數(shù)據(jù)中廊蜒,每個(gè)基因表達(dá)變化范圍對應(yīng)的數(shù)值大小不同溅漾,導(dǎo)致圖片中色彩變化難以顯示基因在不同樣本中的變化趨勢,可以對基因在每個(gè)樣本中基因表達(dá)數(shù)據(jù)進(jìn)行標(biāo)準(zhǔn)化屁倔,使其數(shù)值在一定范圍內(nèi)暮胧,從而實(shí)現(xiàn)熱圖的優(yōu)化叔壤,而控制參數(shù)為scale,對基因(行嗅战,row)進(jìn)行處理
可以與第一張圖比較一下,例如Gene3這一行疟呐,在這張圖中可以看出樣本之間的差異明顯了許多启具。
3.4 pheatmap(test, clustering_distance_rows =
"correlation")
聚類線長度優(yōu)化珊泳,可能不一樣的算法有不一樣的枝長。
clustering_distance_cols同理薯演。
3.5 pheatmap(test, color =
colorRampPalette(c("navy", "white",
"firebrick3"))(10))
設(shè)置顏色跨扮,后面括號里的數(shù)字表示梯度验毡,10就是將這三種顏色設(shè)置為10個(gè)梯度
3.6 pheatmap(test, cluster_row = FALSE)
是否顯示行的聚類晶通,cluster_col同理
3.7 pheatmap(test, legend = FALSE)
是否顯示圖例
4. 顯示色塊的數(shù)值或文本
基本用法:pheatmap(test,display_numbers = TRUE)
?
此外還可添加如下參數(shù)
number_format = "%.3e"表示保留3位小數(shù)录择,且用科學(xué)計(jì)數(shù)法顯示
number_format = "%.3f"表示保留3位小數(shù)隘竭,用小數(shù)顯示
display_numbers除了賦布爾值讼渊,還能賦矩陣(其維度與原矩陣相同),此時(shí)可以人為添加文本(有點(diǎn)像R畫圖的圖層疊加)菱皆。
pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*",
""), 20,10))
legend_breaks設(shè)置圖例的顯示范圍仇轻,間隔為1奶甘;legend_labels重寫刻度的標(biāo)簽, 需與legend_breaks同時(shí)使用。
pheatmap(test, legend_breaks = -1:4,legend_labels = c("0","1e-4", "1e-3","1e-2", "1e-1", "1"))
5. 調(diào)整色塊或文本大小并導(dǎo)出文件
pheatmap(test, cellwidth = 15, cellheight = 12,main = "Example heatmap", fontsize = 8, filename ="test.pdf")
dev.off()
這五個(gè)參數(shù)分別表示:
色塊的寬度方淤、色塊的高度蹄殃、標(biāo)題、行列名及圖例字體的大小讳苦、保存為當(dāng)前工作目錄下的圖片的文件名
6. 行列注釋
對于每一行每一列都添加一些注釋信息鸳谜,本質(zhì)還是"分類"逮京。
annotation_col = data.frame(
? CellType =factor(rep(c("CT1", "CT2"), 5)),
? Time = 1:5
) #
注意rep()的用法;為什么要定義為因子草描;R可以自動(dòng)補(bǔ)全Time變量
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 = "")
> annotation_col
?????? CellType Time
Test1?????? CT1??? 1
Test2?????? CT2??? 2
Test3?????? CT1??? 3
Test4?????? CT2??? 4
Test5?? ????CT1???5
> annotation_row
?????? GeneClass
Gene1????? Path1
Gene2????? Path1
Gene3????? Path1
Gene4????? Path1
Gene5????? Path1
pheatmap(test, annotation_col = annotation_col, annotation_row =
annotation_row)
自定義注釋色塊的顏色
ann_colors = list(
? Time = c("white","firebrick"),
? CellType = c(CT1 = "#1B9E77",CT2 = "#D95F02"),
? GeneClass = c(Path1 ="#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
) #
注意ann_colors是列表
pheatmap(test, annotation_col = annotation_col,annotation_row = annotation_row,
???????? annotation_colors = ann_colors)
?
7. 列名的文本角度調(diào)整
angle_col = "45"穗慕,文本與從左向右水平線的夾角逛绵,只能是“270”, “0”, “45”, “90”, “315”這幾個(gè)值倔韭。
8. 切分熱圖
pheatmap(test, cluster_rows = F, gaps_row = c(10,14), cluster_cols = T,
???????? cutree_col = 4)
gaps_row有效的前提是cluster_rows = F;cutree_col有效的前提是cluster_cols = T
效果圖如下:
?
9. 自定義顯示哪些行列的名字
labels_row = c("", "","", "", "", "", "","", "", "", "", "","", "", "",
?????????????? "","", "Il10", "Il15", "Il1b")
pheatmap(test, labels_row = labels_row)
更多的細(xì)節(jié)就需要在使用的時(shí)候一點(diǎn)點(diǎn)調(diào)試了
參考資料:
http://www.360doc.com/content/18/0316/15/51841481_737529918.shtml