Gregory Jefferis

2005-Mar-05 18:07 UTC

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

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? 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. -- Gregory Jefferis, PhD and: Research Fellow Department of Zoology St John''s College Downing Street Cambridge Cambridge, CB2 3EJ CB2 1TP

Roger Bivand

2005-Mar-05 19:19 UTC

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

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

Gregory Jefferis

2005-Mar-07 08:44 UTC

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

Dear Roger, Thank you for your suggestion and explanation, that was v. helpful. I also found the axTicks() command to help generate the normal sequences for tick marks and labels used in your ''kludge'' eg:>plot(y=-y,x=x, ylim=range(-y), asp=1, axes=FALSE) >box() >axis(1)axis(2, at=axTicks(2), label=axTicks(2)*-1) Many thanks, Greg. On 5/3/05 7:19 pm, "Roger Bivand" <Roger.Bivand at nhh.no> wrote:> 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. >> >>-- Gregory Jefferis, PhD and: Research Fellow Department of Zoology St John''s College Downing Street Cambridge Cambridge, CB2 3EJ CB2 1TP Tel: +44 (0)1223 336683 +44 (0)1223 339899 Fax: +44 (0)1223 336676 +44 (0)1223 337720 gsxej2 at cam.ac.uk