On Mon, 2005-07-04 at 11:35 -0700, yyan liu wrote:> Hi:
> I have a question making side by side boxplot.
> My response is numeric and I want to make a side by
> side boxplot of it accroding to a factor vector. So,
> there are several boxplots on the same plot. Each
> boxplot is with respect to one level for a factor. The
> levels of
> the factor are some characters. When I make the plot,
> the boxplots are arranged according to the alphabetic
> order of the levels. My question is that how I can
> change the order the boxplots are arranged. For
> example,
>
> fac<-c("a","b","c","d")
> boxplot(time.vec~fac,xlab="Events",ylab="Time In
> Days",col="yellow")
>
> Then the boxplots are arranged in the order of
> "a","b","c","d". But for some
reason, I want it to be
> "c","a","d","b". Any suggestion
about this question?
>
> thx a lot!
>
> liu zhong
time.vec <- rnorm(40)
fac <- factor(sample(letters[1:4], 40, replace = TRUE))
# Default ordering
boxplot(time.vec ~ fac, xlab="Events",
ylab="Time In Days", col="yellow")
# Now adjust factor levels as you desire
fac <- factor(fac, levels = c("c", "a", "d",
"b"))
# Redo boxplot with new ordering
boxplot(time.vec ~ fac, xlab="Events",
ylab="Time In Days", col="yellow")
See ?factor for more information. boxplot() sequences the boxes based
upon the factor levels.
HTH,
Marc Schwartz