R語言繪制熱圖(一):heatmap函數(shù)畫熱圖

本篇文章介紹R語言默認(rèn)熱圖函數(shù)heatmap

### 準(zhǔn)備工作
rm(list = ls())
getwd()
setwd("/Users/xuefei/Documents/Heatmap1/")
dir()
# 測試數(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)

首先我們閱讀了說明書(文章最后)

#代碼1
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(<BIOTREE>, ..., scale = \"column\")")
utils::str(hv)
代碼1
#代碼2
## no column dendrogram (nor reordering) at all: 
## 完全沒有列樹圖(或重新排序):
heatmap(A, Colv = NA, col = cm.colors(256), scale = "column",
        RowSideColors = rc, margins = c(5,10),
        xlab = "specification variables", ylab =  "Car Models",
        main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
代碼2
#代碼3
## "no nothing"
heatmap(A, Rowv = NA, Colv = NA, scale = "column",
        main = "heatmap(*, NA, NA) ~= image(t(x))")
round(Ca <- cor(attitude), 2)
symnum(Ca) # simple graphic
heatmap(Ca,               symm = TRUE, margins = c(6,6)) # with reorder()
heatmap(Ca, Rowv = FALSE, symm = TRUE, margins = c(6,6)) # _NO_ reorder()
代碼3
#代碼4
## slightly artificial with color bar, without and with ordering:
## 略人工加色條酱固,無且有順序:
cc <- rainbow(nrow(Ca))
heatmap(Ca, Rowv = FALSE, symm = TRUE, RowSideColors = cc, ColSideColors = cc,
        margins = c(6,6))
heatmap(Ca,     symm = TRUE, RowSideColors = cc, ColSideColors = cc,
        margins = c(6,6))
代碼4


——————** heatmap說明書**—————

Draw a Heat Map

Description #介紹

A heat map is a false color image (basically image(t(x))) with a dendrogram added to the left side and to the top. Typically, reordering of the rows and columns according to some set of values (row or column means) within the restrictions imposed by the dendrogram is carried out.
熱圖是一種假彩色圖像(基本上是圖像(t(x))班眯,在左側(cè)和頂部添加樹狀圖烁巫。通常磁餐,在樹狀圖所施加的限制內(nèi)诊霹,根據(jù)某些值(行或列表示)重新排序行和列脾还。
Usage #使用

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"), ...)
Arguments #參數(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.
# 確定是否以及如何計算行樹圖并重新排序。一個樹狀圖或一個值向量泥张,用于重新排序行樹狀圖或NA媚创,以抑制任何行樹狀圖(和重新排序)鳄橘,或默認(rèn)為空芒炼,請參見下面的“詳細(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ù)有相同的選項炸裆,另外烹看,當(dāng)x是一個方陣時惯殊,Colv = "Rowv"意味著列應(yīng)該與行同等對待(因此,如果沒有行樹圖毕源,也不會有列1)址愿。
distfun 
# 用于計算行和列之間的距離(不同)的函數(shù)响谓。默認(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ù)遏插。默認(rèn)為hclust胳嘲。應(yīng)該將distfun的結(jié)果作為參數(shù)颜屠,并返回一個可以應(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之后將計算的表達式台夺。可用于向繪圖添加組件滚朵。
symm    
logical indicating if x should be treated symmetrically; can only be true when x is a square matrix.
# 邏輯指示是否對x進行對稱處理;只有當(dāng)x是方陣時才成立
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.
# 用于繪圖的列順序是否應(yīng)該反轉(zhuǎn)的邏輯指示,例如匿垄,對于對稱情況漏峰,對稱軸與往常一樣届榄。
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)的字符向量,包含可用于注釋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行的垂直邊欄的顏色名稱峭沦。
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)記蓬豁。當(dāng)前的默認(rèn)值分別只使用行或列的數(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.
# 主標(biāo)題、x軸標(biāo)題和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時)质礼。
verbose 
logical indicating if information should be printed.
# 邏輯指示是否打印信息。
... 
additional arguments passed on to image, e.g., col specifying the colors.
# 其他參數(shù)傳遞給圖像眶蕉,例如,col指定顏色造挽。
Details #詳細(xì)說明

If either Rowv or Colv are dendrograms they are honored (and not reordered). Otherwise, dendrograms are computed as dd <- as.dendrogram(hclustfun(distfun(X))) where X is either x or t(x).
# 如果Rowv或Colv都是樹狀圖,它們將得到尊重(而不是重新排序)饭入。否則,樹狀圖被計算為dd <- as.樹狀圖(hclustfun(distfun(X)))谐丢,其中X為X或t(X)

If either is a vector (of ‘weights’) then the appropriate dendrogram is reordered according to the supplied values subject to the constraints imposed by the dendrogram, by reorder(dd, Rowv), in the row case. If either is missing, as by default, then the ordering of the corresponding dendrogram is by the mean value of the rows/columns, i.e., in the case of rows, Rowv <- rowMeans(x, na.rm = na.rm). If either is NA, no reordering will be done for the corresponding side.
#如果其中一個向量(“權(quán)值”),那么適當(dāng)?shù)臉錉顖D將根據(jù)樹狀圖施加的約束(dd, Rowv)庇谆,在行情況下根據(jù)樹狀圖提供的值重新排序凭疮。如果其中任何一個缺失(默認(rèn)情況下),那么相應(yīng)樹狀圖的順序就是行/列的平均值执解,即寞肖,對于行衰腌,Rowv <- rowMeans(x, na)。rm = na.rm)右蕊。如果其中任何一個是NA,則不需要對對應(yīng)的一側(cè)進行重新排序饶囚。

By default (scale = "row") the rows are scaled to have mean zero and standard deviation one. There is some empirical evidence from genomic plotting that this is useful.
# 默認(rèn)情況下(scale = "row"),這些行被縮放到均值為零萝风,標(biāo)準(zhǔn)差為1。有一些來自基因組繪圖的經(jīng)驗證據(jù)表明這是有用的规惰。

The default colors are not pretty. Consider using enhancements such as the RColorBrewer package.
# 默認(rèn)的顏色并不漂亮。考慮使用增強功能揩晴,例如RColorBrewer package。
Value

Invisibly, a list with components
# 不可見的硫兰,包含組件的列表

rowInd  
row index permutation vector as returned by order.dendrogram.
# 由order.dendrogram返回的行索引排列向量。

colInd  
column index permutation vector.
# 列索引排列向量呻粹。

Rowv    
the row dendrogram; only if input Rowv was not NA and keep.dendro is true.
# 行系統(tǒng)樹圖;只有輸入Rowv不是NA和keep。dendro是真的等浊。
Cold    
the column dendrogram; only if input Colv was not NA and keep.dendro is true.
列系統(tǒng)樹圖;只有當(dāng)輸入Colv不是NA和keep時,dendro才是正確的摹蘑。
Note

Unless Rowv = NA (or Colw = NA), the original rows and columns are reordered in any case to match the dendrogram, e.g., the rows by order.dendrogram(Rowv) where Rowv is the (possibly reorder()ed) row dendrogram.
# 除非Rowv = NA(或Colw = NA),否則原始的行和列在任何情況下都會被重新排序以匹配樹狀圖衅鹿,例如,按順序排列的行大渤。
heatmap() uses layout and draws the image in the lower right corner of a 2x2 layout. Consequentially, it can not be used in a multi column/row layout, i.e., when par(mfrow = *) or (mfcol = *) has been called.
# heatmap()使用布局并在一個2x2布局的右下角繪制圖像。因此耕捞,它不能用于多列/行布局,即俺抽,當(dāng)par(mfrow = *)或(mfcol = *)被調(diào)用時。
Author(s)

Andy Liaw, original; R. Gentleman, M. Maechler, W. Huber, revisions.
# 安迪?Liaw原始;先生较曼,M. Maechler, W. Huber,修訂捷犹。
See Also #另請參閱

image, hclust
Examples ## 示例,非常重要

require(graphics); require(grDevices)
x  <- as.matrix(mtcars)
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(<Mtcars data>, ..., scale = \"column\")")
utils::str(hv) # the two re-ordering index vectors
image.png
## no column dendrogram (nor reordering) at all: 
## 完全沒有列樹圖(或重新排序):
heatmap(x, Colv = NA, col = cm.colors(256), scale = "column",
        RowSideColors = rc, margins = c(5,10),
        xlab = "specification variables", ylab =  "Car Models",
        main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
## "no nothing"
heatmap(x, Rowv = NA, Colv = NA, scale = "column",
        main = "heatmap(*, NA, NA) ~= image(t(x))")


round(Ca <- cor(attitude), 2)
symnum(Ca) # simple graphic
heatmap(Ca,               symm = TRUE, margins = c(6,6)) # with reorder()
heatmap(Ca, Rowv = FALSE, symm = TRUE, margins = c(6,6)) # _NO_ reorder()
## slightly artificial with color bar, without and with ordering:
## 略人工加色條孩哑,無且有順序:
cc <- rainbow(nrow(Ca))
heatmap(Ca, Rowv = FALSE, symm = TRUE, RowSideColors = cc, ColSideColors = cc,
    margins = c(6,6))
heatmap(Ca,     symm = TRUE, RowSideColors = cc, ColSideColors = cc,
    margins = c(6,6))
## 對于變量聚類翠桦,使用基于cor()的距離:
symnum( cU <- cor(USJudgeRatings) )

hU <- heatmap(cU, Rowv = FALSE, symm = TRUE, col = topo.colors(16),
             distfun = function(c) as.dist(1 - c), keep.dendro = TRUE)
## The Correlation matrix with same reordering:
## 相同重排序的相關(guān)矩陣:
round(100 * cU[hU[[1]], hU[[2]]])

## The column dendrogram:
## 列樹圖:
utils::str(hU$Colv)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末胳蛮,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子仅炊,更是在濱河造成了極大的恐慌,老刑警劉巖抚垄,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異呆馁,居然都是意外死亡,警方通過查閱死者的電腦和手機浙滤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進店門气堕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纺腊,“玉大人茎芭,你說我怎么就攤上這事揖膜∶纷” “怎么了?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵摘投,是天一觀的道長。 經(jīng)常有香客問我犀呼,道長薇组,這世上最難降的妖魔是什么外臂? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任律胀,我火速辦了婚禮,結(jié)果婚禮上炭菌,老公的妹妹穿的比我還像新娘。我一直安慰自己黑低,他們只是感情好酌毡,可當(dāng)我...
    茶點故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布蕾管。 她就那樣靜靜地躺著枷踏,像睡著了一般掰曾。 火紅的嫁衣襯著肌膚如雪旭蠕。 梳的紋絲不亂的頭發(fā)上旷坦,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天,我揣著相機與錄音秒梅,去河邊找鬼。 笑死番电,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的漱办。 我是一名探鬼主播这刷,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼娩井,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了洞辣?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤扬霜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后著瓶,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡材原,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了余蟹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,117評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡窑睁,死狀恐怖挺峡,靈堂內(nèi)的尸體忽然破棺而出犹褒,到底是詐尸還是另有隱情,我是刑警寧澤嗡靡,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布病线,位于F島的核電站,受9級特大地震影響送挑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜惕耕,卻給世界環(huán)境...
    茶點故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一诫肠、第九天 我趴在偏房一處隱蔽的房頂上張望司澎。 院中可真熱鬧栋豫,春花似錦挤安、人聲如沸丧鸯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蜂怎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間派敷,已是汗流浹背蛹批。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工篮愉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留差导,地道東北人试躏。 一個月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓设褐,卻偏偏與公主長得像泣刹,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子椅您,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,519評論 25 707
  • 用兩張圖告訴你寡键,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料掀泳? 從這篇文章中你...
    hw1212閱讀 12,693評論 2 59
  • TaoYan 簡介 本文將繪制靜態(tài)與交互式熱圖西轩,需要使用到以下R包和函數(shù):heatmap():用于繪制簡單熱圖的函...
    taoyan閱讀 47,387評論 4 129
  • 今天情人節(jié),但是我半點心情都沒有藕畔。我要完成的任務(wù)有點重,但是還是完成了注服。今天健身沒練到很晚,抓緊時間寫完了生物溶弟。...
    葉兒花閱讀 238評論 0 0
  • ――肖肖 都說緣分天注定,或許得不到的才顯得更加彌足珍貴可很。 沒有更多的...
    肖肖最瀟灑閱讀 365評論 0 0