[R] Ordering x in boxplot
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Thu Aug 16 20:51:48 CEST 2001
>From ?boxplot
x, ...: the data from which the boxplots are to be produced. The data
can be specified as separate vectors, each corresponding to a
component boxplot, or as a single list containing such
vectors. Alternatively a symbolic specification of the form
`x ~ g' can be given, indicating that the observations in the
vector `x' are to be grouped according to the levels of the
`factor' `g'. In this case the argument `data' can be used
Note, *factor* g. So make `drugname' a factor, with the levels
ordered however you like, rather than relying on boxplot.formula to
repair your mistake (and using alphabetical order). ?factor may help.
On Thu, 16 Aug 2001, Scot W McNary wrote:
> 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 order
^^^^^^^^^^^^^^^^^^^^
> 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.
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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