[R] Reverse plot axes with xlim=rev(range(x)) fails with asp=1

Roger Bivand Roger.Bivand at nhh.no
Sat Mar 5 20:19:04 CET 2005


On Sat, 5 Mar 2005, Gregory Jefferis wrote:

> Dear R users,
> 
> I would like to reverse the axes on some xy plots (for example to set the
> origin at the top left rather than the bottom left).  I had planned to use
> something of the following form:
> 
> plot(y=y<-c(20,4,5,6),x=x<-c(10,20,30,40),ylim=rev(range(y)))
> 
> ie reversing ylim to reverse the y axis.  This works fine however I also
> want to use the parameter asp=1 to ensure that equal distances in the x or y
> direction are plotted as such on the screen.  However the simple example I
> showed above now fails:
> 
> > plot(y=y<-c(20,4,5,6),x=x<-c(10,20,30,40),ylim=rev(range(y)),asp=1)
> 
> The actual ylim used is considerably less than it should be, so no points
> appear on the plot.
> 
> Can anyone suggest a simple remedy?

> plot(y=-y,x=x, ylim=range(-y), asp=1, axes=FALSE)
> box()
> axis(1)
> axis(2, at=seq(-20,-5,5), label=seq(20,5,-5))

is a simple kludge, the explanation is touched on in a comment in
src/main/plot.c, after line 564:

 *	The use of asp can have weird effects when axis is an
 *	interpreted function.  It has to be internal so that the
 *	full computation is captured in the display list.

and may be related to the sign of ydelta being dropped in line 652 (for 
the y axis) and Gscale() being possibly called in line 666 with incorrect 
assumptions about whether to add or subtract the correction factor - 
untried!

> 
> Many thanks,
> 
> Greg Jefferis.
> 
> PS I would guess that the origin of this behaviour is that plot.window
> expects xlim (and ylim) to be in the order xmin,xmax (or ymin,ymax) when it
> sets about redefining xlim and ylim if asp!=NA.  If that suggestion is
> correct, I'm not quite sure why the restriction is necessary.
> 
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list