[R] Border-Layout by multiple Plots

Paul Murrell p.murrell at auckland.ac.nz
Wed May 25 03:14:59 CEST 2011


Hi

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:
> http://www.stuber.info/layout_1.jpg
> http://www.stuber.info/layout_2.jpg
>
> The final plots looks like:
> http://www.stuber.info/plot_1.jpg
> http://www.stuber.info/plot_2.jpg
>
>
> 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)))
layout.show(2)

... 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
     m
}

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)))

layout(layoutMatrix)
for (i in 1:8)
     plot(i)
par(mar=rep(0, 4))
for (i in 1:4) {
     plot.new()
     box(lty=3)
}

... 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).

Paul


> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list