加关注

# 云之南

## 日志

### R 绘制 heatmap

2010-10-20 09:02:15|  分类： R&Bioconductor |  标签： |举报 |字号

下载LOFTER 我的照片书  |

http://www.bjt.name/tag/heatmap/

R 绘制 heatmap NBA联盟50位顶级球员的指标表现

？heatmap

A heat map is a graphical representation of data where the values taken by a variable in a two-dimensional map are represented as colors.Heat maps originated in 2D displays of the values in a data matrix. Larger values were represented by small dark gray or black squares (pixels) and smaller values by lighter squares.

R 官网：http://www.r-project.org，它是免费的。官网上面提供了Windows,Mac,Linux版本（或源代码）的R程序。

`nba<- read.csv("http://datasets.flowingdata.com/ppg2008.csv", sep=",") `

Step 2. Sort data

`nba <- nba[order(nba\$PTS),]`

Step 3. Prepare data

`row.names(nba) <- nba\$Name`

`nba <- nba[,2:20] # or nba <- nba[,-1]`

Step 4. Prepare data, again

`nba_matrix <- data.matrix(nba)`

Step 5. Make a heatmap

# R 的默认还会在图的左边和上边绘制 dendrogram，使用Rowv=NA, Colv=NA去掉

`heatmap(nba_matrix, Rowv=NA, Colv=NA, col=cm.colors(256), revC=FALSE, scale='column')`

Step 6. Color selection

`heatmap(nba_matrix, Rowv=NA, Colv=NA, col=heat.colors(256), revC=FALSE, scale="column", margins=c(5,10))`
` `
`Bioinformatics and Computational Biology Solutions Using R and Bioconductor 第10章的`
`例子：`
`Heatmaps, or false color images have a reasonably long history, as has thenotion of rearranging the columns and rows to show structure in the data.They were applied to microarray data by Eisen et al. (1998) and havebecome a standard visualization method for this type of data.A heatmap is a two-dimensional, rectangular, colored grid. It displaysdata that themselves come in the form of a rectangular matrix. The colorof each rectangle is determined by the value of the corresponding entryin the matrix. The rows and columns of the matrix can be rearrangedindependently. Usually they are reordered so that similar rows are placednext to each other, and the same for columns. Among the orderings thatare widely used are those derived from a hierarchical clustering, but manyother orderings are possible. If hierarchical clustering is used, then it iscustomary that the dendrograms are provided as well. In many cases theresulting image has rectangular regions that are relatively homogeneousand hence the graphic can aid in determining which rows (generally thegenes) have similar expression values within which subgroups of samples(generally the columns).The function heatmap is an implementation with many options. In particular,users can control the ordering of rows and columns independentlyfrom each other. They can use row and column labels of their own choosingor select their own color scheme.`
` `

> library("ALL")
> data("ALL")
> selSamples <- ALL\$mol.biol %in% c("ALL1/AF4",
+ "E2A/PBX1")
> ALLs <- ALL[, selSamples]
> ALLs\$mol.biol <- factor(ALLs\$mol.biol)
> colnames(exprs(ALLs)) <- paste(ALLs\$mol.biol,
+ colnames(exprs(ALLs)))

>library("genefilter")
> meanThr <- log2(100)
> g <- ALLs\$mol.biol
> s1 <- rowMeans(exprs(ALLs)[, g == levels(g)[1]]) >
+ meanThr
> s2 <- rowMeans(exprs(ALLs)[, g == levels(g)[2]]) >
+ meanThr
> s3 <- rowttests(ALLs, g)\$p.value < 2e-04
> selProbes <- (s1 | s2) & s3
> ALLhm <- ALLs[selProbes, ]

>library(RColorBrewer)

> hmcol <- colorRampPalette(brewer.pal(10, "RdBu"))(256)
> spcol <- ifelse(ALLhm\$mol.biol == "ALL1/AF4",
+ "goldenrod", "skyblue")
> heatmap(exprs(ALLhm), col = hmcol, ColSideColors = spcol)

>help(heatmap) 查找帮助，看看帮助给提供的例子

http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/r/heatmap/

Using R to draw a Heatmap from Microarray Data

[c]

The first section of this page uses R to analyse an Acute lymphocytic leukemia (ALL) microarray dataset, producing a heatmap (with dendrograms) of genes differentially expressed between two types of leukemia.

There is a follow on page dealing with how to do this from Python using RPy.

The original citation for the raw data is "Gene expression profile of adult T-cell acute lymphocytic leukemia identifies distinct subsets of patients with different response to therapy and survival" by Chiaretti et al. Blood 2004. (PMID: 14684422)

The analysis is a "step by step" recipe based on this paper, Bioconductor: open software development for computational biology and bioinformatics, Gentleman et al. 2004. Their Figure 2 Heatmap, which we recreate, is reproduced here:

Heatmaps from R

Assuming you have a recent version of R (from The R Project) and BioConductor (see Windows XP installation instructions), the example dataset can be downloaded as the BioConductor ALL package.

You should be able to install this from within R as follows:

`> source("http://www.bioconductor.org/biocLite.R")> biocLite("ALL")Running bioCLite version 0.1  with R version  2.1.1 ... `

Alternatively, you can download the package by hand from here or here.

If you are using Windows, download ALL_1.0.2.zip (or similar) and save it. Then from within the R program, use the menu option "Packages", "Install package(s) from local zip files..." and select the ZIP file.

On Linux, download ALL_1.0.2.tar.gz (or similar) and use sudo R CMD INSTALL ALL_1.0.2.tar.gz at the command prompt.

With that out of the way, you should be able to start R and load this package with the library and data commands:
`> library("ALL")Loading required package: BiobaseLoading required package: toolsWelcome to Bioconductor          Vignettes contain introductory material.  To view,          simply type: openVignette()          For details on reading vignettes, see         the openVignette help page.> data("ALL")`

If you inspect the resulting ALL variable, it contains 128 samples with 12625 genes, and associated phenotypic data.

`> ALLExpression Set (exprSet) with         12625 genes        128 samples                 phenoData object with 21 variables and 128 cases         varLabels                cod:  Patient ID                diagnosis:  Date of diagnosis                sex:  Gender of the patient                age:  Age of the patient at entry                BT:  does the patient have B-cell or T-cell ALL                remission:  Complete remission(CR), refractory(REF) or NA. Derived from CR                CR:  Original remisson data                date.cr:  Date complete remission if achieved                t(4;11):  did the patient have t(4;11) translocation. Derived from citog                t(9;22):  did the patient have t(9;22) translocation. Derived from citog                cyto.normal:  Was cytogenetic test normal? Derived from citog                citog:  original citogenetics data, deletions or t(4;11), t(9;22) status                mol.biol:  molecular biology                fusion protein:  which of p190, p210 or p190/210 for bcr/able                mdr:  multi-drug resistant                kinet:  ploidy: either diploid or hyperd.                ccr:  Continuous complete remission? Derived from f.u                relapse:  Relapse? Derived from f.u                transplant:  did the patient receive a bone marrow transplant? Derived from f.u                f.u:  follow up data available                date last seen:  date patient was last seen`

We can looks at the results of molecular biology testing for the 128 samples:

`> ALL\$mol.biol  [1] BCR/ABL  NEG      BCR/ABL  ALL1/AF4 NEG      NEG      NEG      NEG      NEG      [10] BCR/ABL  BCR/ABL  NEG      E2A/PBX1 NEG      BCR/ABL  NEG      BCR/ABL  BCR/ABL  [19] BCR/ABL  BCR/ABL  NEG      BCR/ABL  BCR/ABL  NEG      ALL1/AF4 BCR/ABL  ALL1/AF4      ...  [127] NEG      NEG     Levels: ALL1/AF4 BCR/ABL E2A/PBX1 NEG NUP-98 p15/p16`

Ignoring the samples which came back negative on this test (NEG), most have been classified as having a translocation between chromosomes 9 and 22 (BCR/ABL), or a translocation between chromosomes 4 and 11 (ALL1/AF4).

For the purposes of this example, we are only interested in these two subgroups, so we will create a filtered version of the dataset using this as a selection criteria:

`> eset <- ALL[, ALL\$mol.biol %in% c("BCR/ABL", "ALL1/AF4")] `

The resulting variable, eset, contains just 47 samples - each with the full 12,625 gene expression levels.

This is far too much data to draw a heatmap with, but we can do one for the first 100 genes as follows:

`> heatmap(exprs(eset[1:100,])) `

According to the BioConductor paper we are following, the next step in the analysis was to use the lmFit function (from the limma package) to look for genes differentially expressed between the two groups. The fitted model object is further processed by the eBayes function to produce empirical Bayes test statistics for each gene, including moderated t-statistics, p-values and log-odds of differential expression.

`> library(limma)> f <- factor(as.character(eset\$mol.biol))> design <- model.matrix(~f)> fit <- eBayes(lmFit(eset,design))`

If the limma package isn't installed, you'll need to install it first:

`> source("http://www.bioconductor.org/biocLite.R")> biocLite("limma")Running bioCLite version 0.1  with R version  2.1.1 ... `

We can now reproduce Figure 1 from the paper.

`> topTable(fit, coef=2)              ID         M        A         t      P.Value        B1016     1914_at -3.076231 4.611284 -27.49860 5.887581e-27 56.326537884    37809_at -3.971906 4.864721 -19.75478 1.304570e-20 44.238326939    36873_at -3.391662 4.284529 -19.61497 1.768670e-20 43.9729810865   40763_at -3.086992 3.474092 -17.00739 7.188381e-18 38.646154250    34210_at  3.618194 8.438482  15.45655 3.545401e-16 35.1069211556   41448_at -2.500488 3.733012 -14.83924 1.802456e-15 33.613913389    33358_at -2.269730 5.191015 -12.96398 3.329289e-13 28.764718054    37978_at -1.036051 6.937965 -10.48777 6.468996e-10 21.6021610579 40480_s_at  1.844998 7.826900  10.38214 9.092033e-10 21.27732330      1307_at  1.583904 4.638885  10.25731 1.361875e-09 20.89145`

The leftmost numbers are row indices, ID is the Affymetrix HGU95av2 accession number, M is the log ratio of expression, A is the log average expression, t the moderated t-statistic, and B is the log odds of differential expression.

Next, we select those genes that have adjusted p-values below 0.05, using a very stringent Holm method to select a small number (165) of genes.

`> selected  <- p.adjust(fit\$p.value[, 2]) <0.05> esetSel <- eset [selected, ]`

The variable esetSel has data on (only) 165 genes for all 47 samples . We can easily produce a heatmap as follows (in R-2.1.1 this defaults to a yellow/red "heat" colour scheme):

`> heatmap(exprs(esetSel))`

If you have the topographical colours installed (yellow-green-blue), you can do this:
`> heatmap(exprs(esetSel), col=topo.colors(100)) `

This is getting very close to Gentleman et al.'s Figure 2, except they have added a red/blue banner across the top to really emphasize how the hierarchical clustering has correctly split the data into the two groups (10 and 37 patients).

To do that, we can use the heatmap function's optional argument of ColSideColors. I created a small function to map the eselSet\$mol.biol values to red (#FF0000) and blue (#0000FF), which we can apply to each of the molecular biology results to get a matching list of colours for our columns:

`> color.map <- function(mol.biol) { if (mol.biol=="ALL1/AF4") "#FF0000" else "#0000FF" }> patientcolors <- unlist(lapply(esetSel\$mol.bio, color.map))> heatmap(exprs(esetSel), col=topo.colors(100), ColSideColors=patientcolors)`

Looks pretty close now, doesn't it:

To recap, this is "all" we needed to type into R to achieve this:

`library("ALL")data("ALL")eset <- ALL[, ALL\$mol.biol %in% c("BCR/ABL", "ALL1/AF4")]library("limma")f <- factor(as.character(eset\$mol.biol))design <- model.matrix(~f)fit <- eBayes(lmFit(eset,design))selected  <- p.adjust(fit\$p.value[, 2]) <0.05esetSel <- eset [selected, ]color.map <- function(mol.biol) { if (mol.biol=="ALL1/AF4") "#FF0000" else "#0000FF" }patientcolors <- unlist(lapply(esetSel\$mol.bio, color.map))heatmap(exprs(esetSel), col=topo.colors(100), ColSideColors=patientcolors)`

Heatmaps in R - More Options

One subtle point in the previous examples is that the heatmap function has automatically scaled the colours for each row (i.e. each gene has been individually normalised across patients). This can be disabled using scale="none", which you might want to do if you have already done your own normalisation (or this may not be appropriate for your data):

heatmap(exprs(esetSel), col=topo.colors(75), scale="none", ColSideColors=patientcolors, cexRow=0.5)

You might also have noticed in the above snippet, that I have shrunk the row captions which were so big they overlapped each other. The relevant options are cexRow and cexCol.

So far so good - but what if you wanted a key to the colours shown? The heatmap function doesn't offer this, but the good news is that heatmap.2 from the gplots library does. In fact, it offers a lot of other features, many of which I deliberately turn off in the following example:

`library("gplots")heatmap.2(exprs(esetSel), col=topo.colors(75), scale="none", ColSideColors=patientcolors,          key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)`

By default, heatmap.2 will also show a trace on each data point (removed this with trace="none"). If you ask for a key (using key=TRUE) this function will actually give you a combined "color key and histogram", but that can be overridden (with density.info="none").

Don't like the colour scheme? Try using the functions bluered/redblue for a red-white-blue spread, or redgreen/greenred for the red-black-green colour scheme often used with two-colour microarrays:

`library("gplots")heatmap.2(exprs(esetSel), col=redgreen(75), scale="row", ColSideColors=patientcolors,           key=TRUE, symkey=FALSE, density.info="none", trace="none", cexRow=0.5)`

Heatmaps from Python

So, how can we do that from within Python? One way is using RPy (R from Python), and this is discussed on this page.

P.S. If you want to use heatmap.2 from within python using RPy, use the syntax heatmap_2 due to the differences in how R and Python handle full stops and underscores.

Well, I have also documented how you can load NCBI GEO SOFT files into R as a BioConductor expression set object. As long as you can get your data into R as a matrix or data frame, converting it into an exprSet shouldn't be too hard.

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

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
NULL
, no reordering will be done for the corresponding side.

If
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.

The default colors range from red to white (
heat.colors
) and are not pretty. Consider using enhancements such as the RColorBrewer package, http://cran.r-project.org/src/contrib/PACKAGES.html#RColorBrewer to select better colors.

By default four components will be displayed in the plot. At the top left is the color key, top right is the column dendogram, bottom left is the row dendogram, bottom right is the image plot. When RowSideColor or ColSideColor are provided, an additional row or column is inserted in the appropriate location. This layout can be overriden by specifiying appropriate values for
lmat
,
lwid
, and
lhei
.
lmat
controls the relative postition of each element, while
lwid
controls the column width, and
lhei
controls the row height. See the help page for
layout
for details on how to use these arguments.

Value

Invisibly, a list with components

 rowInd row index permutation vector as returned by order.dendrogram. colInd column index permutation vector. call the matched call rowMeans, rowSDs mean and standard deviation of each row: only present if scale="row" colMeans, colSDs mean and standard deviation of each column: only present if scale="column" carpet reordered and scaled 'x' values used generate the main 'carpet' rowDendrogram row dendrogram, if present colDendrogram column dendrogram, if present breaks values used for color break points col colors used vline center-line value used for column trace, present only if trace="both"or trace="column" hline center-line value used for row trace, present only if trace="both"or trace="row" colorTable A three-column data frame providing the lower and upper bound and color for each bin

Note

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.

heatmap.2()
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.

Author(s)

Andy Liaw, original; R. Gentleman, M. Maechler, W. Huber, G. Warnes, revisions.

Examples
``` library(gplots)
data(mtcars)
x  <- as.matrix(mtcars)
rc <- rainbow(nrow(x), start=0, end=.3)
cc <- rainbow(ncol(x), start=0, end=.3)

##
## demonstrate the effect of row and column dendogram options
##
heatmap.2(x)  ## default - dendrogram plotted and reordering done.
heatmap.2(x, dendrogram="none") ##  no dendrogram plotted, but reordering done.
heatmap.2(x, dendrogram="row") ## row dendrogram plotted and row reordering done.
heatmap.2(x, dendrogram="col") ## col dendrogram plotted and col reordering done.

heatmap.2(x, keysize=2)  ## default - dendrogram plotted and reordering done.

heatmap.2(x, Rowv=FALSE, dendrogram="both") ## generate warning!
heatmap.2(x, Rowv=NULL, dendrogram="both")  ## generate warning!
heatmap.2(x, Colv=FALSE, dendrogram="both") ## generate warning!

## Show effect of row and column label rotation
heatmap.2(x, srtCol=NULL)
heatmap.2(x, srtCol=0,   adjCol = c(0.5,1) )
heatmap.2(x, srtCol=45,  adjCol = c(1,1)   )
heatmap.2(x, srtCol=135, adjCol = c(1,0)   )
heatmap.2(x, srtCol=180, adjCol = c(0.5,0) )
heatmap.2(x, srtCol=225, adjCol = c(0,0)   ) ## not very useful
heatmap.2(x, srtCol=270, adjCol = c(0,0.5) )
heatmap.2(x, srtCol=315, adjCol = c(0,1)   )
heatmap.2(x, srtCol=360, adjCol = c(0.5,1) )

## Show effect of offsetRow/offsetCol (only works when srtRow/srtCol is
## not also present)
heatmap.2(x, offsetRow=0, offsetCol=0)
heatmap.2(x, offsetRow=1, offsetCol=1)
heatmap.2(x, offsetRow=2, offsetCol=2)
heatmap.2(x, offsetRow=-1, offsetCol=-1)

heatmap.2(x, srtRow=0, srtCol=90, offsetRow=0, offsetCol=0)
heatmap.2(x, srtRow=0, srtCol=90, offsetRow=1, offsetCol=1)
heatmap.2(x, srtRow=0, srtCol=90, offsetRow=2, offsetCol=2)
heatmap.2(x, srtRow=0, srtCol=90, offsetRow=-1, offsetCol=-1)

## Show how to use 'extrafun' to replace the 'key' with a scatterplot
lmat <- rbind( c(5,3,4), c(2,1,4) )
lhei <- c(1.5, 4)
lwid <- c(1.5, 4, 0.75)

myplot <- function() {
oldpar <- par("mar")
par(mar=c(5.1, 4.1, 0.5, 0.5))
plot(mpg ~ hp, data=x)
}

heatmap.2(x, lmat=lmat, lhei=lhei, lwid=lwid, key=FALSE, extrafun=myplot)

##
## Show effect of z-score scaling within columns, blue-red color scale
##
hv <- heatmap.2(x, col=bluered, scale="column", tracecol="#303030")

###
## Look at the return values
###
names(hv)

## Show the mapping of z-score values to color bins
hv\$colorTable

## Extract the range associated with white
hv\$colorTable[hv\$colorTable[,"color"]=="#FFFFFF",]

## Determine the original data values that map to white
whiteBin <- unlist(hv\$colorTable[hv\$colorTable[,"color"]=="#FFFFFF",1:2])
rbind(whiteBin[1] * hv\$colSDs + hv\$colMeans,
whiteBin[2] * hv\$colSDs + hv\$colMeans )
##
## A more decorative heatmap, with z-score scaling along columns
##
hv <- heatmap.2(x, col=cm.colors(255), scale="column",
RowSideColors=rc, ColSideColors=cc, margin=c(5, 10),
xlab="specification variables", ylab= "Car Models",
main="heatmap(<Mtcars data>, ..., scale=\"column\")",
tracecol="green", density="density")
## Note that the breakpoints are now symmetric about 0

```

library("gplots")
data_matrix<-data.matrix(x)
heatmap.2(data_matrix,col=redgreen(75),cexCol=0.9,key=T,symkey=F,density.info="none",trace="none")
评论这张

#### 评论

<#--最新日志，群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇，下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->