[R] Getting default aspect ratio from lattice

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed May 16 01:52:58 CEST 2007


On 5/15/07, Waichler, Scott R <Scott.Waichler at pnl.gov> wrote:
> > > How can I get the value of the aspect ratio that is used in
> > a lattice
> > > plot?  In a levelplot for instance, the native units per cm of my x
> > > and y axes are different, and I need to know the aspect
> > ratio so that
> > > I can correctly plot vectors.  I know how to set the aspect in a
> > > high-level lattice function but I can't quite figure out how to get
> > > it.  I would like call to levelplot() without printing
> > anything, get
> > > the aspect ratio, use it to create my vector arrows, then call
> > > levelplot() again with print() to create the final figure.
> >
> > Your question doesn't necessarily have a well defined answer,
> > because the aspect ratio may be computed only on printing
> > (and not even then, as the aspect ratio may change after
> > printing if you resize the device). In fact, this is the
> > default behaviour (aspect = "fill").
>
> Thanks for the help, Deepayan.  Yes, I guess what I am looking for is
> the actual numerical value for aspect.ratio that is used when aspect =
> "fill".  My device is a pdf and I don't resize it.  Could I execute the
> whole plot, including printing it, while saving the aspect.ratio that
> was used, then create the plot again?

Sort of, if you use something like:

getAspect <-
    function(obj)
{
    print(obj)
    trellis.focus("panel", 1, 1, highlight = FALSE)
    cpl <- current.panel.limits(unit = "inches")
    ans <- diff(cpl$ylim) / diff(cpl$xlim)
    trellis.unfocus()
    ans
}

Using this, I get:

> foo <- levelplot(volcano, aspect = "fill")
> x11()
> getAspect(foo)
[1] 1.096661
> dev.off()
> x11("", 5, 9)
> getAspect(foo)
[1] 2.342152
> dev.off()

But if you know the size of your device, you won't do much worse if
you supply a numeric aspect based on that (unless you have a
multipanel plot and the automatic layout calculation is important to
you).

-Deepayan



More information about the R-help mailing list