Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout inside layout in R

Tags:

r

layout

I'm using R to create a heatmap from a matrix using heatmap.2 - and i want to group these images into one big image - What i usually use to achieve this is layout() - but this doesn't work, as heatmap.2 uses layout, and apparently layout does not work recursively.

Does anyone have any suggestions on how to group together 2 images without layout, or how to make layout support recursive 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.

like image 986
dan12345 Avatar asked Dec 04 '11 10:12

dan12345


People also ask

What is the layout R function?

Definition: The layout R function specifies complex plot arrangements. Basic R Syntax: You can find the basic R programming syntax of the layout function below. In the remaining tutorial, I’ll illustrate in three examples how to apply the layout function in R programming.

How does layout layout work?

layout divides the device up into as many rows and columns as there are in matrix mat, with the column-widths and the row-heights specified in the respective arguments. a matrix object specifying the location of the next N figures on the output device. Each value in the matrix must be 0 or a positive integer.

How to arrange the upcoming plots in RStudio?

The first plot will be shown at the upper left side, the second plot will be shown at the lower left side, the third plot will be shown in the upper middle position and so on… Now, we have to apply the layout function to tell RStudio to arrange the upcoming plots as specified in our matrix:

What is the difference between layout and matrix mat?

2 According to ?layout, layout divides the device up into as many rows and columns as there are in matrix mat, with the column-widths and the row-heights specified in the respective arguments. – akrun Aug 7 '16 at 4:26


2 Answers

What follows is a hack that is almost certainly not a perfect solution, but it may get you 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 the layout call:

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

it's a big chunk of code, maybe 30 lines. Now the following code produces two heat maps with dendrograms and keys side by side:

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

Clearly, this will require considerable tweaks to make it look nice (and a larger plotting area; I've squished everything to be a reasonable size to post here).

This is entirely untested. It is likely that using any of the options in the hm3 function that control the appearance of the plot will cause things to go slightly haywire. But this may be a good starting point for your own experimentation to address those issues.

like image 112
joran Avatar answered Nov 14 '22 23:11

joran


What are you planning on doing with the results?

If you just want to compare 2 heatmaps side by side on the screen then rather than combine them into one single plot you can open 2 plotting devices and arrange them side by side to compare (much simpler than creating a single graph):

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

Now drag one to the side of the other using your mouse.

If you want to include the combined graphic in a publication then it may be easiest to create the 2 plots and just set them side by side in whatever program you are using to create the article. If you need them in one file you can still save the 2 heatmaps (or other plots) as 2 files then use tools such as imagemagick, gimp, or inkscape to combine the 2 files into 1 with the graphs side by side.

like image 23
Greg Snow Avatar answered Nov 15 '22 01:11

Greg Snow