When you convert a variable to a factor, it will automatically assign an integer to each level.
Applying the function as.factor applies factor levels in alphanumeric order.
Check out the factor chapter of R4DS,
It actually has a few nice examples of converting variables into factors, plotting them, and then refactoring their levels.