Layout inside layout in R

I use R to create a heatmap from a matrix using heatmap.2 - and I want to group these images into one large image. What I usually use to achieve this is layout () - but this does not work, since heatmap.2 uses the layout, and apparently the layout does not work recursively.

Does anyone have any suggestions for combining two images without a layout or how to make recursive layout layout calls?

mat = matrix(nrow=3,nrow=3,1:9) layout(matrix(nrow=2,ncol=1)) heatmap.2(mat) ## overrides the layout and produces only one plot that takes whole screen heatmap.2(mat) ## still only one image 

thanks.

+6
source share
2 answers

This is followed by a hack, which is almost certainly not an ideal solution, but can help you get started.

Create your own version of the heatmap.2 function called hm3 . In the code for hm3 comment out all the lines between:

  if (missing(lhei) || is.null(lhei)) 

and calling layout :

 layout(lmat, widths = lwid, heights = lhei, respect = FALSE) 

this is a big piece of code, maybe 30 lines. Now the following code creates two heat maps with dendrograms and next to each other:

 x <- as.matrix(mtcars) lhei <- c(1.5, 4,1.5,4) lwid <- c(1.5, 4,1.5,4) layout(rbind(c(4,3,8,7),c(2,1,6,5)), widths = lwid, heights = lhei, respect = FALSE) hm3(x) hm3(x) 

enter image description here

It is clear that this will require significant improvements to make it look beautiful (and the large area of โ€‹โ€‹plotting, I squished everything to be a reasonable size for publication here).

This is completely untested. It is likely that the use of any of the parameters of the hm3 function that control the appearance of the graph will lead to a slightly weaker situation. But this can be a good starting point for your own experiments to solve these problems.

+6
source

What do you plan to do with the results?

If you just want to compare two heat maps next to each other on the screen, and not combine them into one plot, you can open 2 devices for building graphs and arrange them next to each other (much easier than creating one graph):

 heatmap.2(mat1) dev.new() heatmap.2(mat2) 

Now drag one towards the other with the mouse.

If you want to include combined graphics in a publication, then it may be easiest to create 2 stories and just set them next to each other in any program that you use to create an article. If you need them in one file, you can still save 2 heatmaps (or other graphs) as 2 files, and then use tools like imagemagick, gimp or inkscape to combine 2 files in 1 with the graphs nearby.

+1
source

Source: https://habr.com/ru/post/903040/


All Articles