8.9 Change the order of values within a factor - fct_relevel()

The default order for levels with factor() is alphabetical. We often want to reorder the levels in a factor when plotting, or when performing an regression analysis and we want to specify the reference level.

The order can be checked using levels().

## [1] "Died melanoma" "Alive"

The reason “Alive” is second, rather than alphabetical, is it was recoded from “2” and that order was retained. If, however, we want to make comparisons relative to “Alive”, we need to move it to the front by using fct_relevel().

Any number of factor levels can be specified in fct_relevel().