[R] grid: How to merge cells in grid.layout?

Marius Hofert marius.hofert at math.ethz.ch
Sun Sep 23 17:05:04 CEST 2012


Bert Gunter <gunter.berton at gene.com> writes:

> Inline below.
>
> On Sun, Sep 23, 2012 at 1:41 AM, Marius Hofert
> <marius.hofert at math.ethz.ch> wrote:
>> Dear grid expeRts,
>>
>> I would like to create a layout with grid that looks like the following, but with cells (1,1), (1,4), (4,1), and (4,4) removed and cells (2,1) and (3,1) (and (4,2) and (4,3)) combined to one cell (so that contents can easily be centered.
>>
>> How can this be achieved?
>>
>>     require(grid)
>>     gl <- grid.layout(4, 4, widths=unit(c(1, 3, 3, 1), "cm"),
>>                       heights=unit(c(1, 3, 3, 1), "cm"))
>>     grid.show.layout(gl)
>>
>> My goal is to put 4 plots in the larger squares, create some labels in the boxes
>> (1,2), (1,3), (2,4), (3,4), and put common x-axis and y-axis labels in the
>> (combined) boxes (2,1), (3,1) and (4,2), (4,3).
>
> Why don't the "layout.pos.row" and "layout.pos.col" arguments of
> ?viewport not do exactly what you appear to want? Perhaps I
> misunderstand, but I think what you want to do is just create your
> layout as above and then push the viewports you want by suitably
> setting these arguments, and then just draw your plots.

... but I will always have two sub-boxes instead of one single box below the
x-axis and to the left of the y-axis. In a single box for the axis labels, one
can easily center a label. But with two boxes, how do you determine the center
of both? 

>
>
>  If you're serious about using grid, Murrell's Graphics book is
> essential and probably will clarify these issues for you (better than
> I can, anyway).

I know that, thanks for pointing it out. Unfortunately, there is no problem of
the above type mentioned (afaik), that's why I asked.

>
>
>>
>> With layout() one can simply cbind/rbind boxes to produce the desired
>> result. With grid.layout() this seems not be the case. Maybe the workflow is
>> differently here to get the desired result.
>
> You appear to be mistaken. 

I might have described it unsufficiently, but you can do the above with layout()
[I used that trick in many plots before]. The above-mentioned two boxes would
get the same number, so would be treated as one plot region. I was hoping for
something similar with grid.layout. 

It is clear that you can always build your layout with "too many boxes" as a
rectangular region and only address those viewports where plotting should take
place. But I don't see how one could specify to plot in the center of two
adjacent boxes of the same sizes so that the content looks "centered" with
respect to both boxes (viewed as a single box).

> From ?grid.layout:
>
> "This function must NOT be confused with the base R graphics function
> layout. In particular, do not use layout in combination with Grid
> graphics. The documentation for layout may provide some useful
> information and this function should behave identically in comparable
> situations. The grid.layout function has added the ability to specify
> a broader range of units for row heights and column widths, and allows
> for nested layouts (see viewport). "
>
> Cheers,
> Bert
>
>>
>> Cheers,
>>
>> Marius
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list