1. Making better plots
One of the places forcats really shines is in helping you make better graphs. Have you ever made a graph and everything is in the wrong order?
2. Messy scatter plot
What's wrong with this plot? Well, how quickly could you answer "what's the third biggest problem people face?" It's very hard to read this graph right away. For example, to find the problem the largest percentage of people consider an issue, we have to find the point all the way on the right and then connect it back to the text on the y-axis. It's even more difficult to do this for points in the middle.
3. Reordering factors
To fix this, we can use a forcats function, fct_reorder(). Many forcats functions start with fct, which stands for factor. From now on, I'll be saying factor instead of fct.
Factor reorder takes two arguments: the variable you want to reorder and the variable you want to reorder it by. By adding this to our ggplot2 function, we'll have a nicely ordered scatter plot and we can read the y-axis top to bottom.
In that case, we're looking at your unordered variable in relation to another variable. But sometimes we are looking at the variable by itself, such as in a bar chart. Then, you usually want to order by its frequency. Let’s take a look at how we can do this for a frequency bar chart for CurrentJobTitleSelect.
4. Misordered bar frequency
Just like in the previous graph, this one is hard to read. It's fairly easy to see Data Scientist is the most popular job title, and operations research practitioner and data miner the least, but what about the fourth most popular title?
To make the graph easier to read, we can use the forcats function fct_infreq(). This orders a factor by the frequency of each level.
5. Reordering bar chart
Now the graph is much more easy to read; we can easily see that the fourth most popular title is data analyst. To make it even better, we could switch the order, from most to least frequent.
6. Reversing factor levels
To do this, we can use the function fct_rev(). This takes the current levels of the factor and reverses them.
7. Let's practice!
Time to put this into practice.