Re: [R] asp and ylim

From: Martin Maechler <>
Date: Mon, 21 Jul 2008 11:48:13 +0200

>>>>> "DE" == David Epstein <> >>>>> on Mon, 21 Jul 2008 09:42:35 +0100 writes:

    DE> Maybe what I am missing is how to set the "device     DE> region" mentioned in Brian's email.

Play around resizing your graphics window.. This is very instructive, with an 'asp = .' using "traditional graphics" plot().

I'd say the behavior is quite intuitive for some value of "intuitive", but I agree with you that there's also a very valid different value of "intuitive", but the two are very incompatible, and "traditional graphics" in R is the way it is, inherited from a long history of S (and pre S "GRZ"). You should really get the nice book by Paul Murrell on "R Graphics" (Chapman & Hall/CRC) which explains the "traditional graphics" vs the modern "Grid based graphics" (on which 'lattice' or 'ggplot2 are built).

    DE> I have tried various region" mentioned in Brian's
    DE> email. I have tried various searches, but I haven't had
    DE> any luck in finding a reference to "device
    DE> region". However, I'm not sure that changing the device
    DE> region will help if one stays with plot (), because the
    DE> default seems to be that the physical plot region is
    DE> approximately square, and I haven't found a way to
    DE> control the size of the physical plot region. The help
    DE> files for eqscplot() and xyplot () indicate that they
    DE> are meant for scatter plots. But lots of plots are not
    DE> scatter plots. I was only using a scatter plot because
    DE> Rolf's code did so, and he thought I could do what I
    DE> wanted inside plot(), which I can't at the moment.

    DE> Perhaps the best solution is to live with plot() as it
    DE> is. If I need the picture for a paper, I will export
    DE> data to Matlab or Mathematica or Illustrator, where I     DE> can get the control I want.

Hah, you must be kidding!
The control is in R too, of course, you just haven't seen it yet, since it seems you haven't yet been understanding the different graphics models in R sufficiently. The "traditional graphics" you'd be using by plot() [or MASS:::eqscplot()] does indeed define the plot region as a function of graphics device region (plus various margin setting parameters), and so Brian Ripley's answer (of course) was very accurate.

Note that Gabor mentioned a lattice solution, lattice behaving quite differently here {not setting a plot region from the device region}.

For a "paper plot", e.g., pdf() as I'd recommend nowadays, you can set the device region by 'width' and 'height' ; and if you really want to use "traditional graphics" here, do something like

## modified by MM from David Epstein's original example myplot <- function(y, yb = mean(y), ylim = c(-1,1)) {

    ybarv <-, length(y))
    x <- seq_along(y)
    plot(x,y, asp=1, xlab="position",ylab="ybar", type="n", ylim = ylim)     abline(h = ybar)## instead of segments(x[1], ybar, x[ylength], ybar)

    segments(x, ybarv, x,y)
    points  (x, ybarv, pch=21,  bg="white")
    points  (x, y,     pch=19, col="black")

y <- c(1.21, 0.51, 0.14, 1.62, -0.8,

       0.72, -1.71, 0.84, 0.02, -0.12)


## MM: setting device region so that ylim = c(-1,1) "about fits""asp-ex.pdf", height= 3.3, width=10) myplot(y)

    DE> Thanks for all your help. David

    DE> On 20 Jul, 2008, at 23:14, Prof Brian Ripley wrote:

>> Take a look at eqscplot() in package MASS for a different
>> approach.
>> You last para forgets that once you have set the device
>> region and the margins the physical plot region and hence
>> its aspect ratio is determined -- see the figures in 'An
>> Introduction to R'.
>> On Sun, 20 Jul 2008, David Epstein wrote:

    >>> #See David Williams' book "Weighing the odds", p286
    >>> y <- c(1.21, 0.51, 0.14, 1.62, -0.8, 0.72, -1.71, 0.84,
    >>> 0.02, -0.12) ybar <- mean(y) ylength <- length(y) ybarv
    >>> <- rep(ybar, ylength) x <- 1:ylength
    >>> plot(x,y,asp=1,xlab="position",ylab="ybar",type="n",ylim=c(-1,1))
    >>> segments(x[1], ybar, x[ylength], ybar)
    >>> segments(x,ybarv,x,y) points(x, ybarv, pch=21,
    >>> bg="white") points(x,y,pch=19,col="black")
    >>> With asp=1, the value of ylim seems to be totally
    >>> ignored, as in the above code. With asp not set, R plays
    >>> close attention to the value of ylim. This is not
    >>> intuitive behaviour, or is it?
    >>> How can I set the aspect ratio, and simultaneously set
    >>> the plot region? The aspect ratio is one number and the
    >>> plot region is given by four numbers (xleft, xright,
    >>> yleft, yright). Logically, these 5 numbers are
    >>> independent of each other and arbitrary, provided
    >>> xleft<xright and yleft<yright. This should give a
    >>> one-to- one bijection between 5-tuples and plots,
    >>> determined up to a change of scale that is uniform in
    >>> the x- and y-dirctions. My code above shows the (to me)
    >>> obvious attempt, which fails.
    >>> Thanks David
    >>> ______________________________________________
    >>> mailing list
    >>> PLEASE do
    >>> read the posting guide
    >>> guide.html and provide commented, minimal,
    >>> self-contained, reproducible code.

>> --
>> Brian D. Ripley, Professor of
>> Applied Statistics,
>> University of Oxford, Tel: +44 1865 272861 (self) 1 South
>> Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax:
>> +44 1865 272595
    DE> ______________________________________________
    DE> mailing list
    DE> PLEASE do
    DE> read the posting guide

    DE> and provide     DE> commented, minimal, self-contained, reproducible code. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 21 Jul 2008 - 09:58:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jul 2008 - 14:32:01 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive