# [R] Obtaining the true aspect ratio for a lattice plot

Felix Andrews felix at nfrac.org
Wed Mar 10 04:51:21 CET 2010

```Basically you want to achieve an aspect ratio of 0.5 (say) when
specifying aspect = "fill". You can calculate the aspect ratio after a
lattice plot has been displayed:

currAspect <- function() {
trellis.focus("panel", 1, 1, highlight = FALSE)
sz <- current.panel.limits("mm")
trellis.unfocus()
diff(sz\$y) / diff(sz\$x)
}

foo <-
xyplot(rnorm(100) ~ rnorm(100), aspect="fill", ## NB "fill"
xlab="this is some extra text\n
to show that the\n
overall aspect ratio\n
of a plot is not always\n
easily determined by\n
the aspect ratio specified for the data")

dev.new(width = 10, height = 5)
print(foo)
currAspect()
#[1] 0.19003

One problem is that fonts in pdf() seem to be very different to screen
devices. You could probably work out which components of the plot have
a fixed size, and therefore work out the requried aspect ratio... but
I kinda like brute force:

aspectObjective <- function(height, width, target, ...) {
tmp <- tempfile()
pdf(tmp, width = width, height = height, ...)
print(trellis.last.object())
asp <- currAspect()
dev.off()
file.remove(tmp)
abs(asp - target)
}

print(foo)
height <- optimize(aspectObjective, c(1, 20), width = 10,
target = 0.5, tol = 0.01)\$min

height
#[1] 7.6928

pdf("test.pdf", width = 10, height = height)
print(foo)
currAspect()
#[1] 0.50013
dev.off()

On 10 March 2010 07:16, Ryan Hafen <rhafen at stat.purdue.edu> wrote:
> I almost always supply my own aspect ratio when plotting using lattice.
>  When I plot these to pdf, I would like to specify pdf dimensions that will
> result in minimal margins around the plot.  In my application, resorting to
> a pdf cropper after plotting is not an option - I must do it in R.  The
> problem is that I cannot determine the correct aspect ratio for the overall
> plot (accounting for strip labels, keys, titles, labels, etc.) that would
> help me determine the correct pdf dimensions.  I can roughly estimate what
> the overall aspect ratio will be using the layout, etc., but this is not
> good enough.  Is there a way to calculate the true overall aspect ratio for
> the entire plot?
>
> Example: If I make a simple plot and specify aspect=0.5, you would think
> that dimensions width=10, height=5 would be a good approximation, but for
> example:
>
> library(lattice)
> pdf("plot.pdf", width=10, height=5)
> xyplot(rnorm(100) ~ rnorm(100), aspect=0.5,
> xlab="this is some extra text\n
> to show that the\n
> overall aspect ratio\n
> of a plot is not always\n
> easily determined by\n
> the aspect ratio specified for the data")
> dev.off()
>
> doesn't work.  It looks like the true aspect ratio of the resulting
> non-margin area in the pdf is about 0.86.  I'd like to know this in R before
> making the pdf so I can specify dimensions accordingly.  Any ideas?
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Felix Andrews / 安福立
Postdoctoral Fellow
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix.andrews at anu.edu.au
CRICOS Provider No. 00120C
--
http://www.neurofractal.org/felix/

```