R热图类型图与频率图

时间:2014-06-10 15:52:33

标签: r ggplot2 heatmap

我正在尝试创建如下的情节: heatmap with freq plot

我使用ggplot2中的geom_tile()粗略地得到了左图,但是我无法弄清楚如何生成右图和如何将两个图组合在一起。

示例:

tt <- structure(list(Gene = structure(c(3L, 1L, 2L, 4L, 4L, 4L, 2L, 3L, 1L, 3L, 1L, 2L, 1L, 2L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), tumour.sample = structure(c(1L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 3L, 3L, 4L, 4L, 2L), .Label = c("1", "5", "3", "4", "2", "6"), class = "factor"), Effect = c("missense", "missense", "missense", "missense", "missense", "missense", "missense", "nonsense", "missense", "missense", "missense", "missense", "missense", "nonsense", "missense")), .Names = c("Gene", "tumour.sample", "Effect"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 18L), class = "data.frame")
ggplot(tt, aes(x=Gene, y=tumour.sample)) + geom_tile(aes(fill=Effect)) + theme(axis.text.x = element_text(angle = -90, hjust = 0))

这样做的最佳方法是什么?

目前热图并没有顶部的标签,而且方框也不是正方形。

1 个答案:

答案 0 :(得分:1)

要获得侧面图,您可能需要转换数据。这是一个粗略的开始(使用上面的tt数据)

p1<-ggplot(tt, aes(x=Gene, y=tumour.sample)) + 
    geom_tile(aes(fill=Effect)) + 
    theme(axis.text.x = element_text(angle = -90, hjust = 0)) + 
    scale_fill_discrete(guide=F) 

#transform and summarize
rs<-do.call(rbind, Map(function(a,b) {
    data.frame(s=b, i=seq_along(a), eff=sort(a))},
lapply(split(tt, tt$tumour.sample), '[[', "Effect"), 
levels(factor(tt$tumour.sample))))

#make side plot
p2<-ggplot(rs, aes(x=i, y=s)) + 
    geom_tile(aes(fill=eff)) +
    theme(axis.text.y=element_blank(), axis.title.y=element_blank()) 

#print both plots
grid.arrange(p1, p2, ncol=2)

enter image description here