繁体   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