2014-02-24 25 views
3

我有以下這種樹狀圖的熱圖。如何擴展熱圖中的樹狀圖.2

完整的數據是here

問題是左邊的樹狀圖被擠壓。如何在不改變熱圖的列大小的情況下解開(展開)它?

enter image description here

它與這個碼生成:

#!/usr/bin/Rscript 
library(gplots); 
library(RColorBrewer); 


plot_hclust <- function(inputfile,clust.height,type.order=c(),row.margins=70) { 

    # Read data 
    dat.bcd <- read.table(inputfile,na.strings=NA, sep="\t",header=TRUE); 


    rownames(dat.bcd) <- do.call(paste,c(dat.bcd[c("Probes","Gene.symbol")],sep=" ")) 
    dat.bcd <- dat.bcd[,!names(dat.bcd) %in% c("Probes","Gene.symbol")] 
    dat.bcd <- dat.bcd 

    # Clustering and distance function 
    hclustfunc <- function(x) hclust(x, method="complete") 
    distfunc <- function(x) dist(x,method="maximum") 


    # Select based on FC, as long as any of them >= anylim 

    anylim <- 2.0 
    dat.bcd <- dat.bcd[ apply(dat.bcd, 1,function(x) any (x >= anylim)), ] 


    # Clustering functions 
    height <- clust.height; 

    # Define output file name 
    heatout <- paste("tmp.pafc.heat.",anylim,".h",height,".pdf",sep=""); 


    # Compute distance and clusteirn function 
    d.bcd <- distfunc(dat.bcd) 
    fit.bcd <- hclustfunc(d.bcd) 


    # Cluster by height 
    #cutree and rect.huclust has to be used in tandem 
    clusters <- cutree(fit.bcd, h=height) 
    nofclust.height <- length(unique(as.vector(clusters))); 

    myorder <- colnames(dat.bcd); 
    if (length(type.order)>0) { 
    myorder <- type.order 
    } 

    # Define colors 
    #hmcols <- rev(brewer.pal(11,"Spectral")); 
    hmcols <- rev(redgreen(2750)); 
    selcol <- colorRampPalette(brewer.pal(12,"Set3")) 
    selcol2 <- colorRampPalette(brewer.pal(9,"Set1")) 
    sdcol= selcol(5); 
    clustcol.height = selcol2(nofclust.height); 

    # Plot heatmap 
    pdf(file=heatout,width=20,height=50); # for FC.lim >=2 
    heatmap.2(as.matrix(dat.bcd[,myorder]),Colv=FALSE,density.info="none",lhei=c(0.1,4),dendrogram="row",scale="row",RowSideColors=clustcol.height[clusters],col=hmcols,trace="none", margin=c(30,row.margins), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0),keysize=0.3); 
    dev.off(); 


} 
#-------------------------------------------------- 
# ENd of functions 
#-------------------------------------------------- 

plot_hclust("http://pastebin.com/raw.php?i=ZaGkPTGm",clust.height=3,row.margins=70); 
+0

你是什麼意思_squished_? – jbaums

+0

多數樹狀圖,我們無法清楚地看到樹的高度。 – pdubois

+2

這不僅僅是因爲大多數節點比最底層的節點彼此之間的關係更密切?爲了「減弱」樹狀結構的「壓扁」部分,似乎需要進一步擴大已經未受影響的地區。否則,高度不再是相對的。也許你可以調整高度(例如'sqrt'),以將大值拉近大多數值,但這可能會導致誤導樹。或者,使整個陰謀更寬。 – jbaums

回答

6

在你的情況的數據具有長尾,預計用於基因表達數據(對數正態)。

data <- read.table(file='http://pastebin.com/raw.php?i=ZaGkPTGm', 
        header=TRUE, row.names=1) 

mat <- as.matrix(data[,-1]) # -1 removes the first column containing gene symbols 

正如可以從位數分配看到具有最高表達的基因從1.5延伸至上述300

quantile(mat) 

#  0%  25%  50%  75% 100% 
# 0.000 0.769 1.079 1.544 346.230 

當上未縮放數據所執行的分級聚類結果樹狀圖可能會顯示偏向最高表達式的值,如您的示例所示。這在很多情況下(reference)都適用於對數或z分數轉換。您的數據集包含values == 0,這是對數轉換問題,因爲log(0)未定義。

Z分數轉換reference)是內heatmap.2實現,但需要注意的是函數計算距離矩陣和縮放運行前的數據聚類算法是非常重要的。因此,選項scale='row'不會影響聚類結果,請參閱我以前的帖子(differences in heatmap/clustering defaults in R)以獲取更多詳細信息。

我建議你達到你的數據以前運行heatmap.2

# scale function transforms columns by default hence the need for transposition. 
z <- t(scale(t(mat))) 

quantile(z) 

#   0%  25%  50%  75%  100% 
# -2.1843994 -0.6646909 -0.2239677 0.3440102 2.2640027 

# set custom distance and clustering functions 
hclustfunc <- function(x) hclust(x, method="complete") 
distfunc <- function(x) dist(x,method="maximum") 

# obtain the clusters 
fit <- hclustfunc(distfunc(z)) 
clusters <- cutree(fit, 5) 

# require(gplots) 
pdf(file='heatmap.pdf', height=50, width=10) 
heatmap.2(z, trace='none', dendrogram='row', Colv=F, scale='none', 
      hclust=hclustfunc, distfun=distfunc, col=greenred(256), symbreak=T, 
      margins=c(10,20), keysize=0.5, labRow=data$Gene.symbol, 
      lwid=c(1,0.05,1), lhei=c(0.03,1), lmat=rbind(c(5,0,4),c(3,1,2)), 
      RowSideColors=as.character(clusters)) 
dev.off() 

另外,查看其他職位herehere,這解釋瞭如何通過lmatlwid並設置熱圖的佈局lhei參數。

產生的熱圖如下所示(行和列標籤被省略):

enter image description here

+0

非常感謝。最後一個問題,此處的尺度函數z < - t(scale(t(mat)))與Z分數轉換權是否相同? – pdubois

+1

@pdubois,不客氣。是的,'scale'應用z-score轉換。看到這篇文章[在R中創建z分數](http://stackoverflow.com/questions/6148050/creating-z-scores)。注意它在輸入矩陣上的用法:**基於行的**變換't(scale(t(mat)))'或**基於列的**變換'scale(mat)'。請關注[使用z分數]這篇文章(http://stats.stackexchange.com/questions/36076/is-a-heat-map-of-gene-expression-more-informative-if-z-scores -are-used-instead-o)可視化基因表達的變化。 – TWL

0

據我所知,你可能在很在您的數據設置一些異常值(對象底部)。請嘗試以下操作:

  1. 從數據刪除異常值設定
  2. logscale你的距離,給不太重視極端值