簡體   English   中英

在grob圖中組合熱圖和樹形圖

[英]Combining a heatmap and a dendrogram in a grob plot

我正在嘗試繪制heatmap以及行dendrogram ,我操縱(修剪分支的數量),使用grid.draw對齊。

這是我的數據:

set.seed(10)
mat     <- matrix(rnorm(24*10,mean=1,sd=2),nrow=24,ncol=10,dimnames=list(paste("g",1:24,sep=""),paste("my.expriment.sample",1:10,sep="")))
dend    <- as.dendrogram(hclust(dist(mat)))
row.ord <- order.dendrogram(dend)
mat     <- matrix(mat[row.ord,],nrow=24,ncol=10,
             dimnames=list(rownames(mat)[row.ord],colnames(mat)))
mat.df  <- reshape2::melt(mat,value.name="expr",varnames=c("gene","sample"))

情節的heatmap部分:

require(ggplot2)
map.plot <- ggplot(mat.df,aes(x=sample,y=gene)) + geom_tile(aes(fill=expr)) +
    scale_fill_gradient2("expr",high="darkred",low="darkblue") + theme_bw() + 
    theme(legend.key=element_blank(),legend.position="right", axis.text.y=element_blank(), axis.ticks.y=element_blank(), 
    panel.border=element_blank(), strip.background=element_blank(),  axis.text.x=element_text(angle=45,hjust=1,vjust=1), 
    legend.text=element_text(size=5), legend.title=element_text(size=8), legend.key.size=unit(0.4,"cm"))

這使:

在此輸入圖像描述

請注意長列標簽 - 這與我現實中的標簽類似。

這是我操作和繪制dendrogram

depth.cutoff <- 11
dend <- cut(dend,h=depth.cutoff)$upper
require(dendextend)
gg.dend <- as.ggdend(dend)

#change vertical segments that lead to leaves
distinctColors <- function(n) {
  if (n <= 8) {
    res <- brewer.pal(n, "Set2")
  } else {
    res <- hcl(h=seq(0,(n-1)/(n),length=n)*360,c=100,l=65,fixup=TRUE)
  }
}

cluster.cols <- distinctColors(nrow(gg.dend$labels))
leaf.heights <- dplyr::filter(gg.dend$nodes,!is.na(leaf))$height
leaf.seqments.idx <- which(gg.dend$segments$yend %in% leaf.heights)
gg.dend$segments$yend[leaf.seqments.idx] <- max(gg.dend$segments$yend[leaf.seqments.idx])
gg.dend$segments$col[leaf.seqments.idx] <- cluster.cols

#change labels
gg.dend$labels$label <- 1:nrow(gg.dend$labels)
gg.dend$labels$y <- max(gg.dend$segments$yend[leaf.seqments.idx])
gg.dend$labels$x <- gg.dend$segments$x[leaf.seqments.idx]
gg.dend$labels$col <- cluster.cols
dend.plot <- ggplot(gg.dend,labels=F)+scale_y_reverse()+coord_flip()+annotate("text",size=10,hjust=0,x=gg.dend$label$x,y=gg.dend$label$y,label=gg.dend$label$label,colour=gg.dend$label$col)

這使:

在此輸入圖像描述

試着按照這個例子 ,我這樣做:

require(gtable)
plot.grob <- ggplotGrob(dend.plot)
plot.grob <- gtable_add_cols(plot.grob,unit(1,"cm"))
plot.grob <- gtable_add_grob(plot.grob,ggplotGrob(map.plot),t=1,l=ncol(plot.grob),b=1,r=ncol(plot.grob))
grid.newpage()
grid.draw(plot.grob)

但是這個搞得一團糟: 在此輸入圖像描述

任何想法如何得到dend.plot與對齊heatmap的一部分map.plot使得下分支dend.plot的底部與對齊的heatmap ,而不是列標簽的底部?

cowplot非常善於調整ggplots。

library(cowplot)
plot_grid(dend.plot, map.plot, align = 'h')

在此輸入圖像描述

另外,嘗試使用更短的示例(為什么我需要一個超級詳細的theme調用?),並確保它實際上在一個干凈的會話中運行。

暫無
暫無

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

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