[R] boxplot: reverse y-axis order

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Nov 21 20:38:54 CET 2010

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 I
> haven't found a solution.  Using the data and code below, I generate the
> 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 tried
> the experiment approach to no avail, meaning I've placed rev(...) around
> 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 the
> order
> boxplot(value~factor(depthM,levels=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=T,outline=F)

So if you want to reverse, either specify the levels in reverse order or 
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

More information about the R-help mailing list