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

Ryan Hafen rhafen at stat.purdue.edu
Tue Mar 9 21:16:36 CET 2010

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:

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

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  

More information about the R-help mailing list