如何在 R 中的主热图一侧添加额外的单列热图 [英] How to add an additional single column heatmap at the side of main heatmap in R

查看:61
本文介绍了如何在 R 中的主热图一侧添加额外的单列热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本:

library("gplots")
mydata <- mtcars
mydata.nr <- nrow(mydata)
mydata.newval <-  data.frame(row.names=rownames(mydata),new.val=-log(runif(mydata.nr)))

# Functions
hclustfunc <- function(x) hclust(x, method="complete")
distfunc <- function(x) dist(x,method="euclidean")

# Set colors
hmcols <- rev(redgreen(256));

# Plot the scaled data
heatmap.2(as.matrix(mydata),dendrogram="row",scale="row",col=hmcols,trace="none", margin=c(8,9), hclust=hclustfunc,distfun=distfunc);

生成以下热图:

现在给出一个新的 data.frame,其中包含每辆车的新值:

Now given a new data.frame which contain new values for each cars:

mydata.nr <- nrow(mydata)
mydata.newval <-  data.frame(row.names=rownames(mydata),new.val=-log(runif(mydata.nr)))

我想创建一个单列热图,在行名称旁边放置渐变灰色.我怎样才能在 R heatmap.2 中实现这一点?

I want to create a single column heatmap with gradient gray positioned next to row names. How can I achieve that in R heatmap.2?

推荐答案

这符合您的要求吗?您可以使用 RowSideColors 选项在热图的一侧添加一列.

Does this do what you want? You can use the RowSideColors option to add a column to the side of the heatmap.

new.vals = mydata.newval[,1]
mydata.newval$scaled = ( new.vals - min(new.vals) ) / 
                       ( max(new.vals) - min(new.vals) )
mydata.newval$gray = gray( mydata.newval$scaled )

heatmap.2( as.matrix(mydata), 
           dendrogram = "row", scale = "row",
           col = hmcols, trace = "none", 
           margin = c(8,9), 
           hclust = hclustfunc, distfun = distfunc,
           RowSideColors=mydata.newval$gray )

如果你想要热图和标签之间的灰色列,没有一个简单的使用 heatmap.2 做到这一点的方法;我不认为它是为这样的目的.将其组合在一起的一种方法是使灰度值从 10 到 11(或超出其余数据范围的某个值).然后您将更改映射到中断的颜色(参见 此处).然而,这会让你的钥匙看起来很时髦.

If you want the gray column in between the heatmap and the labels, there isn't a simple way to do that with heatmap.2; I don't think it was designed for such purposes. One way to hack it together would be to make the gray values go from 10 to 11 (or something out of the range of the rest of the data). Then you would change the colors mapped to the breaks (see here). However, this would make your key look pretty funky.

# heatmap.2 does the clustering BEFORE the scaling. 
# Clustering after scaling might give different results
# heatmap.2 also reorders the dendrogram according to rowMeans.
# (Code copied directly from the heatmap.2 function)
x = as.matrix(mydata)
Rowv = rowMeans(x, na.rm = TRUE)
hcr = hclustfunc(distfunc(x))
ddr = as.dendrogram(hcr)
ddr = reorder(ddr, Rowv) # the row dendrogram

# Scale the data as heatmap.2 does
rm = rowMeans(x, na.rm = TRUE)
x = sweep(x, 1, rm)
sx =  apply(x, 1, sd, na.rm = TRUE)
x = sweep(x, 1, sx, "/")

# add the new data as a column
new.vals = mydata.newval[,1]
new.vals.scaled = ( new.vals - min(new.vals) ) / 
                  ( max(new.vals) - min(new.vals) ) # scaled from 0 to 1
x = cbind( x, gray = max(x) + new.vals.scaled + 0.1 )

# make the custom breaks and colors
edge = max(abs(x-1.1))
breaks = seq(-edge,edge+1.1,length.out=1000)
gradient1 = greenred( sum( breaks[-length(breaks)] <= edge ) )
gradient2 = colorpanel( sum( breaks[-length(breaks)] > edge ), "white", "black" )
hm.colors = c(gradient1,gradient2)

hm = heatmap.2( x, col=hm.colors, breaks=breaks,
           scale="none", 
           dendrogram="row", Rowv=ddr,
           trace="none", margins=c(8,9) )

虽然这个 hack 有效,但我会寻找一个更强大的解决方案,使用更灵活的包,使用 grid 包来处理不同的视口.

Although this hack works, I would look for a more robust solution using more flexible packages that play with different viewports using the grid package.

这篇关于如何在 R 中的主热图一侧添加额外的单列热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆