Learn R Programming

lattice (version 0.10-9)

reorderFactor: 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

reorderFactor(x, X, FUN = mean, ...,
              order = is.ordered(x))

Arguments

x
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 x, whose subset of values for each unique level of x determines the eventual order of that level
FUN
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 x
...
extra arguments supplied to FUN
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 FUN applied to X grouped by x. The levels are ordered such that the values returned by FUN are in increasing order.

See Also

xyplot, Lattice