[R] How to reverse the sequence of axis Y ??

Marc Schwartz MSchwartz at mn.rr.com
Sat Dec 17 18:55:19 CET 2005


The key here is to set the 'ylim' argument so that the range of the y
axis is rev()ersed.

So:
 
 # Set 'x'
 x <- 0:10

 # set xlim and ylim
 # set ylim to rev() the range() of 'x'
 plot(x, xlim = c(0, 10), ylim = rev(range(x)))

If you actually want to have the 'x' axis value of 0 in the lower left
hand corner as is shown below, you need to adjust the axis style, which
by default ("r") is expanded by 4% of the range of the x and y values
(or xlim and ylim). So use:

  plot(x, xlim = c(0, 10), ylim = rev(range(x)), yaxs = "i", xaxs = "i")

See ?par for more information on 'xaxs' and 'yaxs' relative to axis
styles.

Note also, that once you have reversed the axis range, you can still set
the axes so that the default tickmarks and labels are not drawn and then
use axis() as per normal for custom labels, etc.

  plot(x, xlim = c(0, 10), ylim = rev(range(x)), yaxs = "i", xaxs = "i",
       axes = FALSE)

  # Draw the x axis
  axis(1, at = 0:10)

  # Draw the y axis
  axis(2, at = seq(0, 10, 2), las = 2)

  # Put a box around the plot
  box()


Note that the y axis labels are properly reversed in synch with the
prior range setting for 'ylim' without further manipulation being
required.

HTH,

Marc Schwartz


On Sat, 2005-12-17 at 18:04 +0100, Kristel Joossens wrote:
> Hello Klebyn,
> 
> It might be that a better solution exists, but here is something that 
> might help you. (I used that all y's are positive!)
> 
> R> maxy=ceiling(max(y))
> R> plot(x,maxy-y,axes=FALSE)
> R> axis(1)
> R> axis(2,0:maxy,seq(maxy,0,-1))
> 
> If not all your y's are positive define `miny' and take then
> R> axis(2,miny:maxy,seq(maxy,miny,-1))
> 
> Best regards and good luck,
> Kristel
> 
> klebyn wrote:
> >  
> > 
> > Hello R-users!
> > 
> > My (simple?) doubt: How to reverse the sequence of axis Y ??
> > 
> > the diagram below illustrate my idea...
> > 
> > (default)
> >    |
> >    |
> > .  |
> > .  |
> > .  |
> > 3  |
> > 2  |
> > 1  |
> > 0  +----------------------------
> >    0 1 2 3 ...
> > 
> > 
> > 
> > like I want...
> >  
> > 0  |
> > 1  |
> > 2  |
> > 3  |
> > .  |
> > .  |
> > .  |
> >    |
> >    +----------------------------
> >    0 1 2 3 ...
> > 
> > 
> > thanks in advance
> > 
> > 
> > klebyn




More information about the R-help mailing list