reorder

reorder the levels of a factor

reorder the levels of "class" by their mean score on "hwy"
data(ggplot2::hwy)
qplot(reorder(class, hwy), hwy, data=mpg)