Re: [R] Border-Layout by multiple Plots

From: Paul Murrell <>
Date: Wed, 25 May 2011 13:14:59 +1200


On 19/05/2011 9:43 p.m., Stuber Thomas TA.I_BB_BS.0701 wrote:
> Hello R-Community
> I did a lot of research, but i found no answer for my issue.
> I try to plot multiple Plots. I do this with the layout() function.
> The following two pictures show the defined layout:
> The final plots looks like:
> So far is all right. Now my problem is, that I'm not able to make the borders as needed. See the layout_1.jpg and layout_2.jpg again. The colored lines represents the needed borders.
> I tried to draw lines into every plot to bulit a global border. But it's a hack job and it's "ugly". I also tried to make the layout with the par() or the split.screen() functions, but without success. To interleave layouts is also not possible in R (or does anybody know a way to do it?)
> Does anyone can help me with this issue?

You can take advantage of the fact that plot 'i' is allocated regions based on the *range* of rows and columns in which 'i' appears in the layout matrix. For example, in the following simple layout, the second plot is in the middle and the first plot occupies the entire page ...

layout(rbind(c(1, 0, 0),

              c(0, 2, 0),
              c(0, 0, 1)))

... the only problem being that the matrix you need could get a bit complex, but because the layout is very regular it's possible to write simple functions to help out. Here's a somewhat programmatic approach to generating a slight simplification of your layout ...

plotMatrix <- function(i) {

     matrix(c(0, i), nrow=3, ncol=3)

regionMatrix <- function(i, nr=1, nc=1) {

     m <- matrix(0, nrow=nr*3, ncol=nc*3)
     m[1, 1] <- i
     m[nr*3, nc*3] <- i


layoutMatrix <- rbind(cbind(plotMatrix(1), plotMatrix(2),

                             plotMatrix(3), plotMatrix(4)),
                       cbind(plotMatrix(5), plotMatrix(6),
                             plotMatrix(7), plotMatrix(8))) +
     rbind(cbind(regionMatrix(9, nc=2), regionMatrix(10, nc=2)),
           cbind(regionMatrix(11, nc=2), regionMatrix(12, nc=2)))

for (i in 1:8)

par(mar=rep(0, 4))
for (i in 1:4) {


... OR you could use the 'grid' package, where layouts are more flexible and this sort of thing is easy (especially if your plots really are only simple, unadorned barplots and scatterplots like in the example images you posted).


> [[alternative HTML version deleted]]
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.

Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
New Zealand
64 9 3737599 x85392

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 25 May 2011 - 01:17:14 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 25 May 2011 - 03:50:09 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive