To display two heat maps in one PDF file together in R

I am trying to display two or more heatmaps next to each other in the same png or pdf. Layout or mfcol do not work in this case. Can someone please help me with this.

+4
source share
4 answers

As indicated in the reference document for the heat map .2,

'heatmap.2 ()' uses 'layout' and draws an 'image' in the lower right corner of the 2x2 layout. Therefore, it cannot be used in the layout of several columns / rows, that is, when "par (mfrow = *)" or '(mfcol = *)'.

The same is true for the heat map.

+3
source

Here is one option using the recently introduced gridGraphics package,

enter image description here

library(gridGraphics) library(grid) heatmap(as.matrix(mtcars)) library(gridGraphics) grab_grob <- function(){ grid.echo() grid.grab() } g <- grab_grob() grid.newpage() # library(gridExtra) # grid.arrange(g,g, ncol=2, clip=TRUE) lay <- grid.layout(nrow = 1, ncol=2) pushViewport(viewport(layout = lay)) grid.draw(editGrob(g, vp=viewport(layout.pos.row = 1, layout.pos.col = 1, clip=TRUE))) grid.draw(editGrob(g, vp=viewport(layout.pos.row = 1, layout.pos.col = 2, clip=TRUE))) upViewport(1) 
+2
source

After the same problem, I came up with the following solution:

1) Use ggplot2 to make your heat map with a dendrogram, like here: Play the lattice dendrogram graph with ggplot2 , and then arrange it using the multipot () function ( http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_%_ 28ggplot2% 29 / )

2) However, this is a lot of work, and I wanted to stick with the core function heatmap (). The following is easy (though not just R) and works on Linux if you have imagemagick installed:

 m <- matrix(runif(10^2), ncol=10) for (i1 in 1:4) { ifile <- paste0(i1,'_heatmap.pdf') pdf(ifile) heatmap(m) d <- dev.off() } system('montage -geometry 100% -tile 2x2 ./*_heatmap.pdf outfile.pdf') 
+1
source

Here's how to do it. These are very hacks, but I think that when a function does not do what you want to do, the best solution is to force it to do it anyway.
The heatmap.2 function contains three lines in the middle of the code:

 ... op <- par(no.readonly = TRUE) on.exit(par(op)) layout(lmat, widths = lwid, heights = lhei, respect = FALSE) ... 

Because of them, you cannot use layout and par(mar=...) since it overrides it. Copy the heatmap.2 code into a new function (say heatmap.3 ) and delete these three lines:

 heatmap.3 <- function(... #etc etc with the exact same code minus those 3 lines 

Then your code for creating your two heat maps will be, for example:

 layout(rbind(c(4,3,8,7),c(2,1,6,5)), widths = c(1,2,1,2), heights = c(1,2), respect = FALSE) heatmap.3(x) heatmap.3(y) 

When preparing the layout, remember that in terms of the heat dissipation map, first the heat map itself is displayed, then the dendrogram "row", then the dendrogram "col" and, finally, the histogram, therefore, the order is from top to bottom, from left to right - 4, 3, 2, 1 when both heat cards side by side become 4, 3, 8, 7, 2, 1, 6, 5.

0
source

All Articles