Learn R Programming

lattice (version 0.10-2)

reorder.factor: Reorder levels of a factor

Description

Reorders the levels of a factor (or vector) depending on values of a second variable, usually numeric. The most common use is to reorder levels of a factor to be used in a plot in some visually meaningful way, by default in increasing order of the mean of the second variable grouped by the factor.

Usage

reorder.factor(Factor, X, Function = mean, ...,
               order = is.ordered(Factor))

Arguments

Factor
a vector from which a factor will be created, or a factor (possibly ordered) whose levels will be reordered
X
a vector of the same length as Factor, whose subset of values for each unique level of Factor determines the eventual order of that level
Function
a function whose first argument is a vector and returns a scalar, to be applied to each subset of X determined by the levels of Factor
...
extra arguments supplied to Function
order
logical, whether return value will be an ordered factor rather than a factor

Value

  • A factor or an ordered factor (depending on the value of order), with the order of the levels determined by Function applied to X grouped by Factor. The levels are ordered such that the values returned by Function are in increasing order.

See Also

xyplot, Lattice