0

I am trying to create a plot like the following: heatmap with freq plot

I have roughly got the left plot using geom_tile() from ggplot2, but I can't work out how to one generate the right-hand graph and how to get the two plots together.

Example:

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))

What is the best approach to do this?

At the moment the heatmap doesn't have the labels at the top and the boxes are not square as well.

yoda230
  • 449
  • 6
  • 14
  • This question is all about programming and I think should be posted on SO. – Stat Jun 10 '14 at 16:36
  • Getting the plots together is easy with `gridExtra::grid.arrange`. See [this question](http://stackoverflow.com/q/5226807/903061) for details and other options. – Gregor Thomas Jun 10 '14 at 17:47

1 Answers1

1

To get the side plot, you would probably need to transform the data. Here's a rough start (using the tt data from above)

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295