[R] margin control in lattice package

Peter Ehlers ehlers at ucalgary.ca
Sun Sep 26 00:00:59 CEST 2010


On 2010-09-25 8:59, Jonathan Flowers wrote:
> Hi all,
>
> I am difficulty with simple layout of plots in the lattice package
>
> I have created a series of levelplots and would like to plot them to a
> single device, but need to reduce the margin areas.  This is easily
> accomplished with par(oma) and par(mar) in the base graphics package but I
> am having problems finding the equivalent features in the lattice package.
> Ideally, I would like to reduce the amount of white space among plots in the
> following example. Thanks in advance.
>
> library(lattice)
> p1<- levelplot( matrix(c(1:25),nr=5,nc=5),row.
> values=1:5,column.values=1:5)
> p2<- levelplot(matrix
> (rnorm(25),nr=5,nc=5),row.values=1:5,column.values=1:5)
> p3<- levelplot( matrix(c(1:25),nr=5,nc=5),row.values=1:5,column.values=1:5)
> p4<- levelplot(matrix
> (rnorm(25),nr=5,nc=5),row.values=1:5,column.values=1:5)
>
> print(p1,split=c(1,1,2,2),more=T)
> print(p2,split=c(2,1,2,2),more=T)
> print(p3,split=c(1,2,2,2),more=T)
> print(p4,split=c(2,2,2,2))
>
> Thanks in advance,
>
> Jonathan

Here are a couple of things you can play with.

First, the default for the matrix method of levelplot() is to
produce square plots using the argument aspect="iso". So
unless you set aspect=<some other value or "fill">, you can
only reduce the outer white space at the expense of
increasing the inner white space. To reduce the outer white
space but keep the aspect="iso", you can set the size of
the graphics device and then fiddle with the top.padding
and/or bottom.padding components of the layout.heights
parameter. Something like this:

## some simple data
  m <- matrix(1:25, nr=5)

## create 4 (identical for illustration only) plots
  p1 <- p2 <- p3 <- p4 <- levelplot(m, aspect="iso",
        par.settings=list(layout.heights=list(top.padding=-2)))

## open a trellis device (I'm on Windows)
  trellis.device(windows, height=6, width=7)

## print the plots
  print(p1, split=c(1,1,2,2), more=TRUE)
  print(p2, split=c(2,1,2,2), more=TRUE)
  print(p3, split=c(1,2,2,2), more=TRUE)
  print(p4, split=c(2,2,2,2))


## alternatively, use aspect="fill" and adjust size in the
## print() calls
  p1 <- p2 <- p3 <- p4 <- levelplot(m, aspect="fill")

  trellis.device(windows, height=6, width=7)
  print(p1, split=c(1,1,2,2),
     panel.height=list(x=2, units="in"), more=TRUE)
  print(p2, split=c(2,1,2,2),
     panel.height=list(x=2, units="in"), more=TRUE)
  print(p3, split=c(1,2,2,2),
     panel.height=list(x=2, units="in"), more=TRUE)
  print(p4, split=c(2,2,2,2),
     panel.height=list(x=2, units="in"))


Probably the best way, if your levels are roughly the same
for all plots, is to convert your data to a data frame,
define a 4-level factor, and create a standard 4-panel plot
instead of using the 'split' argument.

   -Peter Ehlers



More information about the R-help mailing list