Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Borders around groups of plots

Tags:

r

I've got 16 plots on a page, arranged in a 4x4 grid. I'd like to have a border around each set of 2x2, but can't work out how to do it.

I initially used layout(matrix(c(1,2,5,6,3,4,7,8,9,10,13,14,11,12,15,16), 4, 4, byrow=TRUE)) to create the correct layout, but, as far as I know, there's no way of creating margins that extend beyond both the current plot and any subsequent plot that the margin would overlap.

The 2nd approach I tried was to use split.screen(), thinking that any oma() settings would only apply to the current screen, however, it appears to apply to the whole window; for example, the following code produces a border at the outer of the whole display area, not screen(1):

depths <- c(1:10)
split.screen(c(2,2))
screen(1)
par(oma=c(1,1,1,1))
plot(depths)
box("inner", lty="dotted", col="green")

Trying various things with either box("inner") or box("outer") after setting oma settings doesn't create the desired result.

Are there any other obvious approaches I should be trying?

Thanks,
Chris

like image 883
ChrisW Avatar asked Sep 28 '11 14:09

ChrisW


2 Answers

Try using box("figure"):

depths <- c(1:10)
split.screen(c(2,2))
screen(1)
box("figure")
split.screen(c(2,2))
par(cex=0.5)
screen(5)
plot(depths)
like image 133
Aaron left Stack Overflow Avatar answered Sep 28 '22 08:09

Aaron left Stack Overflow


Here is a way using layout:

layout(matrix(c(1,2,5,6,3,4,7,8,9,10,13,14,11,12,15,16), 4, 4, byrow=TRUE))
replicate(16, hist(rnorm(100)))
par(xpd=NA)
rect( grconvertX(0.005, from='ndc'), grconvertY(0.505, from='ndc'),
     grconvertX(0.495, from='ndc'), grconvertY(0.995, from='ndc'))
rect( grconvertX(0.005, from='ndc'), grconvertY(0.005, from='ndc'),
     grconvertX(0.495, from='ndc'), grconvertY(0.495, from='ndc'))
rect( grconvertX(0.505, from='ndc'), grconvertY(0.505, from='ndc'),
     grconvertX(0.995, from='ndc'), grconvertY(0.995, from='ndc'))
rect( grconvertX(0.505, from='ndc'), grconvertY(0.005, from='ndc'),
     grconvertX(0.995, from='ndc'), grconvertY(0.495, from='ndc'))

Adjust it to your preferences.

like image 28
Greg Snow Avatar answered Sep 28 '22 08:09

Greg Snow