fct_reorder {forcats} | R Documentation |
fct_reorder
is useful for 1d displays where the factor is mapped to
position; fct_reorder2
for 2d displays where the factor is mapped to
a non-position aesthetic.
fct_reorder(f, x, fun = median, ..., .desc = FALSE) fct_reorder2(f, x, y, fun = last2, ..., .desc = TRUE)
f |
A factor |
x, y |
The levels of |
fun |
An summary function. It should take one vector for
|
... |
Other arguments passed on to |
.desc |
Order in descending order? Note the default is different
between |
boxplot(Sepal.Width ~ Species, data = iris) boxplot(Sepal.Width ~ fct_reorder(Species, Sepal.Width), data = iris) boxplot(Sepal.Width ~ fct_reorder(Species, Sepal.Width, .desc = TRUE), data = iris) chks <- subset(ChickWeight, as.integer(Chick) < 10) chks <- transform(chks, Chick = fct_shuffle(Chick)) if (require("ggplot2")) { ggplot(chks, aes(Time, weight, colour = Chick)) + geom_point() + geom_line() # Note that lines match order in legend ggplot(chks, aes(Time, weight, colour = fct_reorder2(Chick, Time, weight))) + geom_point() + geom_line() + labs(colour = "Chick") }