[R] Ordering x in boxplot

Scot W McNary smcnary at charm.net
Thu Aug 16 20:10:40 CEST 2001


Hi R-users,

I've managed to make a very nice set of boxplots borrowing some code in
example(boxplot).  I have data on age of first use for a variety of
substances for a group of people.  My graph is intended to show
side-by-side boxplots of the distribution of age of use for various
subgroups (here, gender) for each substance (there are 11).  Here is my
heavily borrowed code:

# uses example(boxplot) code to do m/f vs. age at first use
# first set of boxplots
bertha1<-boxplot(as.numeric(agestart) ~ drugname,
        # only for 1=Males; draw boxes at 1,2,...11 with a -.2 offset
        data=start, subset=gender==1, at= 1:11 -.2,    
        #set box width, fill color and suppress x axis         
        boxwex=.20, col="pink", xaxt = "n",                  
        # title        
        main="Male/Female vs. Age at First Use for Various Drugs",
        # axis labels        
        xlab="", ylab="Age at First Use", ylim=c(0,25))   
# second set of boxplots
bertha2<-boxplot(as.numeric(agestart) ~ drugname,
        # boxes at 1,2,..11 with a +.2 offset
        data=start, subset=gender==2, at= 1:11 +.2,      
        # add to previous plot but suppress axes (offset makes em messy)        
        boxwex=.20, col="gray", add=TRUE, axes=FALSE)          
# add legend to match        
legend(9.5, 5, c("Male", "Female"), fill=c("Pink", "Gray"))       
# add custom axis that makes names perpendicular
axis(1, labels=c(bertha1$names), at=1:11, las=2)          


I like the looks of this graph quite a lot, but there is one feature I'd
like to include but as of yet don't know how.  As can be seen in the
boxplot.formula, the 11 substances are displayed across the x axis by the
variable 'drugname' which is a character variable.  The default ordering
for the box printing appears to me to be alphabetical.  This works well,
however, this graph could be made even more interesting by ordering the x
axis differently.  I'd like to order it so that the overall earliest age
of use appears first on the left hand side, and the overall latest age of
use on the far right hand side.  Ordering in this way, I think, will show
both subgroup variation within substance and a nice left to right trend in
age of first use across the substances.

I have a numeric variable (drugnumber) I've already created that indexes
the character variable (drugname) from earliest to latest, so I suppose I
could substitute:

boxplot(as.numeric(agestart) ~ drugnumber, # vs. of char var. 'drugname'

and then supply my own tickmark labels.  But I was wondering how one might
reorder the x axis more generally?

My checks in the reference manual and help pages haven't yielded any fruit
on this particular feature, but I'll stipulate I may be looking where the
light is better.  Suggestions most appreciated.

Thanks,

Scot McNary 

         _              
platform i386-pc-mingw32
arch     x86            
os       Win32          
system   x86, Win32     
status                  
major    1              
minor    3.0            
year     2001           
month    06             
day      22             
language R              
 




--
  Scot W. McNary  email:smcnary at charm.net   


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list