Search code examples
rplotheatmappheatmap

Multiple panel plots with pheatmap


I am trying to do multipanel plots one panel being a heatmap using layout to place plots. I've been drawing heatmaps with pheatmap which provides a very convenient color scheme among other things.

The code for pheatmap is available here.

When I try using pheatmap in this way it always plots on a new page. I imagine this is because of its use of the grid package? Is there a way I can do this with pheatmap?

Example code to produce a heatmap next to a barplot but which doesn't since the heatmap gets plotted on a new page below:

xlay=layout( matrix(c(2,2,1),nrow=1) )
layout.show(xlay)
barplot(rnorm(8),horiz=T)
pheatmap(matrix(rnorm(80),nrow=8))

Solution

  • Make your bar plot in ggplot

    bar <- ggplot()  
    

    Assign both the barplots and heatmap to a variable

    heat <- pheatmap(matrix(rnorm(80),nrow=8))
    

    then use gridExtra package to make panel plot the heatmap is saved as an object and you can plot it again by assessing the 4th item in the object

    grid.arrange(bar, heat[[4]], nrow = 1)