簡體   English   中英

如何在heatmap.2()中為原始數據指定色標

[英]How to assign your color scale on raw data in heatmap.2()

我的數據看起來像這樣:

                         Name    h1    h2    h3    h4    h5
1            1420468_at_Asb17 0.000 2.328 0.000 0.000 0.000
2    1430261_at_1700024J04Rik 1.236 2.050 0.000 0.000 0.000
3           1431788_at_Fabp12 0.000 2.150 0.000 0.000 0.587
4    1433187_at_B230112I24Rik 0.000 2.240 1.343 0.000 1.383
5        1434430_s_at_Adora2b 0.000 2.006 1.459 0.000 1.272
6           1435217_at_Gm7969 0.727 2.350 1.494 0.976 0.000
7          1436717_x_at_Hbb-y 0.000 2.712 0.000 0.000 0.000
8            1440859_at_Akap6 0.000 2.053 0.000 0.000 1.840
9              1442625_at_--- 0.000 2.064 1.173 0.000 1.035
10           1443715_at_Rbm24 0.969 2.219 0.000 0.000 0.000
11             1445520_at_--- 0.000 2.497 0.000 0.000 0.000
12          1446035_at_Gm7173 0.000 3.869 0.000 0.000 0.000
13   1446597_at_6820445E23Rik 1.000 2.000 0.000 0.000 0.000
14          1448925_at_Twist2 0.000 2.089 0.938 0.000 0.000
15        1449711_at_Atp6v1e1 0.605 2.363 2.350 1.094 0.976
16          1455931_at_Chrna3 0.000 2.354 0.000 0.000 0.000
17 1457647_x_at_1600023N17Rik 0.000 2.734 0.000 0.000 1.812
18             1458975_at_--- 0.000 2.079 0.000 0.000 0.000
19             1459862_at_--- 0.727 2.606 0.000 0.000 1.151

注意這個數據(和實際的數據)沒有負值,正值可以大到100左右。

我想要做的是用我自己指定的色標和方案繪制熱圖:

  1. 當值為0時,將其設置為白色。
  2. 當值為== 1時,將其設置為黑色。
  3. 當值> 1時,將其設置為紅色陰影。
  4. 當值<1且> 0時,將其設置為綠色陰影。

也沒有使用任何數據縮放或內置的z-score轉換。 我怎樣才能做到這一點?

我目前的代碼是這樣的:

library(gplots)

# Read data
dat <- read.table("http://dpaste.com/1501148/plain/",sep="\t",header=T);
rownames(dat) <- dat$Name
dat <- dat[,!names(dat) %in% c("Name")]

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

#  Define colours
hmcols <- rev(redgreen(2750));

# Plot 
pdf("~/Desktop/tmp.pdf",height=10)
heatmap.2(as.matrix(dat),Colv=FALSE,dendrogram="row",scale="row",col=hmcols,trace="none", margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0),keysize=1);
dev.off()

這將生成以下圖表,其中使用默認的z-score行縮放。

在此輸入圖像描述

這里的關鍵是理解heatmap.2col參數與breaks參數結合使用。

看看下面的代碼和圖,看看我的意思。

library(gplots)
set.seed(100)
dat = matrix( rexp(25,1/2), ncol=5 )
breaks = 0:5
col = c("green","blue","red","yellow","brown")
heatmap.2( dat, breaks=breaks, col=col )

在此輸入圖像描述

如您所見, n斷裂必須有n-1種顏色。 對於您的特定問題,問題是將正確的顏色映射到中斷。 我正在使用scale="none"選項,因為@josilber指出。

breaks = seq(0,max(dat),length.out=1000)
gradient1 = colorpanel( sum( breaks[-1]<=1 ), "white", "green", "black" )
gradient2 = colorpanel( sum( breaks[-1]>1 ), "black", "red" )
hm.colors = c(gradient1,gradient2)

heatmap.2(as.matrix(dat),scale="none",breaks=breaks,col=hm.colors,
          Colv=FALSE,dendrogram="row",trace="none", 
          margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0))

在此輸入圖像描述

另一種選擇是有兩個漸變:綠色 - >黑色和黑色 - >紅色。 然后,您可以通過將它們設置為NA並設置na.color="white"來手動將零值設置na.color="white"

breaks = seq(0,max(dat),length.out=1000)
gradient1 = colorpanel( sum( breaks[-1]<=1 ), "green", "black" )
gradient2 = colorpanel( sum( breaks[-1]>1 ), "black", "red" )
hm.colors = c(gradient1,gradient2)

dat[dat==0] = NA
heatmap.2(as.matrix(dat),scale="none",breaks=breaks,col=hm.colors,na.color="white",
          Colv=FALSE,dendrogram="row",trace="none", 
          margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0))

最后,您可以手動編輯零值的漸變。

breaks = seq(0,max(dat),length.out=1000)
gradient1 = colorpanel( sum( breaks[-1]<=1 ), "green", "black" )
gradient2 = colorpanel( sum( breaks[-1]>1 ), "black", "red" )
hm.colors = c(gradient1,gradient2)
hm.colors[1] = col2hex("white")

heatmap.2(as.matrix(dat),scale="none",breaks=breaks,col=hm.colors,na.color="white",
          Colv=FALSE,dendrogram="row",trace="none", 
          margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0))

在此輸入圖像描述

記錄折疊更改

另一方面,您可能會看到倍數變化或某種類型的比率。 在制作熱圖時繪制對數倍數變化是相當常見的。 我將零值“灰顯”出來。

dat[dat==0] = NA
heatmap.2( as.matrix(log2(dat)), col=greenred(100), 
           scale="none", na.color="grey",symbreaks=TRUE,
           Colv=FALSE,dendrogram="row",trace="none", 
           margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0))

在此輸入圖像描述

有關@ josilber的好解決方案的解釋:

此代碼hmcols <- c(colfunc1(200), colfunc2(200*(max(dat) - 1)))生成長度為774的字符向量(通過length(hmcols)看到)。 因此,這意味着應該定義775個中斷。 默認情況下, heatmap.2函數會產生n+1中斷,其中ncol參數中使用的向量的長度。 因此,確定了斷點和顏色的數量 ,但hmcols <- c(colfunc1(200), colfunc2(200*(max(dat) - 1)))如何正確地將顏色映射到斷點? 訣竅是巧妙地創建了hmcols向量。 第一個漸變中的顏色數為200.由於沒有明確定義breaks ,我們知道中斷的間隔是均勻的。 由於第一個梯度從0變為1並且有200個中斷,因此每個中斷的寬度應為0.005(或1/200)。 由於第二個梯度從1到3.869( max(dat) ),因此應該有2.869 / 0.005 = 573.8個中斷(向上舍入時有574個中斷)。 請注意, 200*(max(dat) - 1))執行此計算; 它輸出573.8。 因此,有200 + 574種顏色映射到正確的休息時間,一切正常!

我認為這里有兩件事。 首先是如何擺脫z分數。 這可以使用scale="none"參數來進行heatmap.2

另一個問題圍繞着你想要的漸變。 我依靠colorRampPalette來完成這一部分。 下面,我構建一個白色漸變 - >綠色 - >黑色表示值0到1然后變黑 - >紅色表示值1 - > max(dat)

library(gplots)

# Read data
dat <- read.table("http://dpaste.com/1501148/plain/",sep="\t",header=T);
rownames(dat) <- dat$Name
dat <- dat[,!names(dat) %in% c("Name")]

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

#  Define colours
colfunc1 <- colorRampPalette(c("white", "green", "black"))
colfunc2 <- colorRampPalette(c("black", "red"))
hmcols <- c(colfunc1(200), colfunc2(200*(max(dat) - 1)))

# Plot 
pdf("~/Desktop/tmp.pdf",height=10)
heatmap.2(as.matrix(dat),Colv=FALSE,dendrogram="row",scale="none",col=hmcols,trace="none", margin=c(5,10), hclust=hclustfunc,distfun=distfunc,lwid=c(1.5,2.0),keysize=1);
dev.off()

在此輸入圖像描述

這里的主題是為每個休息時間定義休息和特定顏色。 這可以通過使用heatmap.2函數來實現。

library(gplots)
library(RColorBrewer)

#Table formatting 
rownames(df)<-df[,1] #setting row names
df<-as.matrix(df[,-1])

# Defining breaks for the color scale!
##defining color scale

myCol <- c("white",colorRampPalette(c("green","darkgreen"))(100),"black",colorRampPalette(c("red","darkred"))    (100)) 
#you can change the colors here. 
#It is important to have the total number of colors defined for all the breaks. 
#i.e if the number of breaks is 100, then there should be 99 colors defined.
#You can change the gradient of the shades by changing no of splots, 
#I have used 100 here

##defining breaks
myBreaks <- c(-1,0,seq(1e-5,1-1e-5,length=100),1,seq(1+1e-3,200,length=100)) 

#set your break start/end and the length here
# I have set it as per your requirements here. Teh shades

#Plotting heatmap 

pdf("temporal_data.pdf",width=8,height=8)
hm <- heatmap.2(df, scale="none", Colv=NA,
                col = myCol, ## using your colors
                breaks = myBreaks, ## using your breaks
                dendrogram = "row",  ## row dendograms
                , cexRow=1, cexCol=1, key=FALSE,
                margins = c(2, 12),trace="none")
legend("topleft", fill = c("white","green","black","red"),
       legend = c("0", "0.0001 to 0.999", "1",">1"),cex=1,horiz =TRUE)
dev.off()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM