[R] boxplot: reverse y-axis order

S Ellison S.Ellison at lgc.co.uk
Mon Nov 22 14:02:17 CET 2010

A simple alternative is to use "at" to contrl plot locations:

boxplot( ..., at=rev(1:nlevels(depthM)))

which just rearranges where they are plotted.

 x <- gl(5, 5)
boxplot(y~x, horizontal=TRUE)
boxplot(y~x, at=rev(1:nlevels(x)), , horizontal=TRUE)

Steve E

>>> Uwe Ligges <ligges at statistik.tu-dortmund.de> 21/11/2010 19:38:54

On 21.11.2010 20:30, emorway wrote:
> Hello,
> Searching this forum has enabled me to get pretty far in what I'm
trying to
> do.  However, there is one more manipulation I would like to make and
> haven't found a solution.  Using the data and code below, I generate
> plot produced by the last command.  If possible I would like to
reverse the
> order of the y-axis (bearing in mind horizontal=T) so that 0 is
plotted at
> the upper most part of the y-axis and 3 at the axis intercepts.  I've
> the experiment approach to no avail, meaning I've placed rev(...)
> various arguments but with wrong results.
> Thanks,
> Eric
> df<-read.table(textConnection("Field Date AvgWTD Region variable
value hole
> depth depthM
> 204 17-Aug-00 2.897428989 US R1 NA R 1 0


> 9A 09-Aug-00 1.482089996 US C6 NA C 6 1.8
> 9B 01-Jun-01 1.409700036 US C6 NA C 6 1.8
> 9B 09-Aug-00 3.837660074 US C6 NA C 6 1.8"),header=T)
> closeAllConnections()
> #The folling call doesn't preserve the correct spacing between data
> boxplot(value~depthM,data=df,horizontal=T,outline=F)
> #The following command preserves correct spacing, but need to reverse
> order

So if you want to reverse, either specify the levels in reverse order
use rev() as in:

boxplot(value ~ factor(depthM,
     levels = rev(c(0.0,0.3,0.6,0.9,1.2,1.5,1.8,2.1,2.4,2.7,3))),
     data = df, horizontal = TRUE, outline = FALSE)

Uwe Ligges

R-help at r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

This email and any attachments are confidential. Any use...{{dropped:8}}

More information about the R-help mailing list