1. Reordering factors
In this chapter, we'll discuss how you can manipulate your factor variables to make them more useful. You'll learn how to reorder levels, change the names of the levels, and collapse levels into one another or into a new level called "other".
In the previous chapter, we focused on categorical variables, which don’t have an inherent order. In those cases, it made sense to order the variable either by its own frequency or by another variable.
2. Misordered graphs
Sometimes though we have ordinal variables, which do have an inherent order. For example, take a look at this graph. What’s wrong with it? Well, this is a frequency plot but the scale is misordered. We would expect, if we wanted to understand how often people use natural language processing at work, that we would be able to read this chart left to right, assuming it goes from least often to most often. But in this case, “rarely" is in the middle and "most of the time" is on the left, so anyone looking at this graph in a glance is going to be misled.
3. Corrected graph
We can use fct_relevel() to solve this problem. fct_relevel() allows you to manually enter the order of levels for your factor. Let’s add fct_relevel() to our ggplot2 function and see what happens.
Success, the chart is fixed! We know have, left to right, rarely, sometimes, often, and most of the time, putting it in order of least frequent to most frequent.
4. fct_reorder()
Another tip is that if only one level was out of order, you don’t have to re-enter all the levels. You can specify where you want to move an item within the current order. The default is that the levels will become the first ones. So, if we wanted to move often and most of the time to the front, we could have those be the arguments to factor relevel.
5. Additional arguments
If we want to move the levels somewhere other than at the front, you can use the argument “after” to move it to a specific place. For example, "after” equals 2 will move it after the second item, and after equals Inf will move it to the end. You can do this with multiple levels at a time as well. In this case, because there are four items, after equals 2 returns the same as after equals Inf when we're moving two items.
6. Let's practice!
Let's try it out!