[R] Re-order levels of a categorical (factor) variable
wdunlap at tibco.com
Thu Jan 22 01:36:18 CET 2015
Are you sure the factors of T are in the order you think they are? (Are you
sure you are using the expected version of T.) Use print(levels(T)) to
timeCats <- c("Presurgery", "Day 30", "Day 60", "Day 180", "Day 365")
d <- data.frame(T = factor(rep(timeCats, 11:15), levels=timeCats),
boxplot(Y ~ T, data=d)
and the boxes and labels are in the order given in 'timeCats'.
On Wed, Jan 21, 2015 at 2:37 PM, Ravi Varadhan <ravi.varadhan at jhu.edu>
> I have a fairly elementary problem that I am unable to figure out. I have
> a continuous variable, Y, repeatedly measured at multiple times, T. The
> variable T is however is coded as a factor variable having these levels:
> c("Presurgery", "Day 30", "Day 60", "Day 180", "Day 365").
> When I plot the boxplot, e.g., boxplot(Y ~ T), it displays the boxes in
> this order: c("Day 180", "Day 30", "Day 365", "Day 60", "Presurgery").
> Is there a way to control the order of the boxes such that they are
> plotted in a particular order that I want, for example: c("Presurgery",
> "Day 30", "Day 60", "Day 180", "Day 365")?
> More generally, is there a simple way to redefine the ordering of the
> categorical variable such that this ordering will be used in whatever
> operation is done? I looked at relevel, reorder, etc., but they did not
> seem to be applicable to my problem.
> Thanks for any help.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help