R语言绘制热图(一):heatmap函数画热图

本篇文章介绍R语言默认热图函数heatmap

### 准备工作
rm(list = ls())
getwd()
setwd("/Users/xuefei/Documents/Heatmap1/")
dir()
# 测试数据
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)),在左侧和顶部添加树状图。通常,在树状图所施加的限制内,根据某些值(行或列表示)重新排序行和列。
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 #参数
x   
numeric matrix of the values to be plotted.
# 要绘制的值的数值矩阵
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,以抑制任何行树状图(和重新排序),或默认为空,请参见下面的“详细信息”。
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参数有相同的选项,另外,当x是一个方阵时,Colv = "Rowv"意味着列应该与行同等对待(因此,如果没有行树图,也不会有列1)。
distfun 
# 用于计算行和列之间的距离(不同)的函数。默认为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.
# 当Rowv或Colv不是树状图时,用于计算层次聚类的函数。默认为hclust。应该将distfun的结果作为参数,并返回一个可以应用as.dendrogram的对象。
reorderfun  
function(d, w) of dendrogram and weights for reordering the row and column dendrograms. The default uses reorder.dendrogram.
# 树状图的函数(d, w)和重排序行和列树状图的权值。默认使用reorder.dendrogram。
add.expr    
expression that will be evaluated after the call to image. Can be used to add components to the plot.
# 在调用image之后将计算的表达式。可用于向绘图添加组件。
symm    
logical indicating if x should be treated symmetrically; can only be true when x is a square matrix.
# 逻辑指示是否对x进行对称处理;只有当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.
# 用于绘图的列顺序是否应该反转的逻辑指示,例如,对于对称情况,对称轴与往常一样。
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.
# 字符,指示值是否应该在行方向或列方向中居中和缩放,或不缩放。如果symm为false,默认为“row”,否则为“none”。
na.rm   
logical indicating whether NA's should be removed.
# 逻辑指示是否应该删除NA。
margins 
numeric vector of length 2 containing the margins (see par(mar = *)) for column and row names, respectively.
# 长度为2的数字向量,分别包含列名和行名的边距(参见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.
# 正数,用作cex。轴在为行或列轴标记。当前的默认值分别只使用行或列的数量。
labRow, labCol  
character vectors with row and column labels to use; these default to rownames(x) or colnames(x), respectively.
# 使用行和列标签的字符向量;这些默认为rownames(x)或colnames(x)。
main, xlab, ylab    
main, x- and y-axis titles; defaults to none.
# 主标题、x轴标题和y轴标题;默认为没有。
keep.dendro 
logical indicating if the dendrogram(s) should be kept as part of the result (when Rowv and/or Colv are not NA).
# 逻辑指示树图是否应该作为结果的一部分保留(当Rowv和/或Colv不是NA时)。
verbose 
logical indicating if information should be printed.
# 逻辑指示是否打印信息。
... 
additional arguments passed on to image, e.g., col specifying the colors.
# 其他参数传递给图像,例如,col指定颜色。
Details #详细说明

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.
#如果其中一个向量(“权值”),那么适当的树状图将根据树状图施加的约束(dd, Rowv),在行情况下根据树状图提供的值重新排序。如果其中任何一个缺失(默认情况下),那么相应树状图的顺序就是行/列的平均值,即,对于行,Rowv <- rowMeans(x, na)。rm = na.rm)。如果其中任何一个是NA,则不需要对对应的一侧进行重新排序。

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.
# 默认情况下(scale = "row"),这些行被缩放到均值为零,标准差为1。有一些来自基因组绘图的经验证据表明这是有用的。

The default colors are not pretty. Consider using enhancements such as the RColorBrewer package.
# 默认的颜色并不漂亮。考虑使用增强功能,例如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.
# 行系统树图;只有输入Rowv不是NA和keep。dendro是真的。
Cold    
the column dendrogram; only if input Colv was not NA and keep.dendro is true.
列系统树图;只有当输入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布局的右下角绘制图像。因此,它不能用于多列/行布局,即,当par(mfrow = *)或(mfcol = *)被调用时。
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:
## 相同重排序的相关矩阵:
round(100 * cU[hU[[1]], hU[[2]]])

## The column dendrogram:
## 列树图:
utils::str(hU$Colv)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,133评论 25 707
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,724评论 2 59
  • TaoYan 简介 本文将绘制静态与交互式热图,需要使用到以下R包和函数:heatmap():用于绘制简单热图的函...
    taoyan阅读 47,479评论 4 129
  • 今天情人节,但是我半点心情都没有。我要完成的任务有点重,但是还是完成了。今天健身没练到很晚,抓紧时间写完了生物。...
    叶儿花阅读 240评论 0 0
  • ――肖肖 都说缘分天注定,或许得不到的才显得更加弥足珍贵。 没有更多的...
    肖肖最潇洒阅读 371评论 0 0