Facet labels and order
1. Facet labels and order
Now that you understand how to use facets for one or more categorical variables, let's take a look at labeling and arranging facets properly.2. A new dataframe
We'll begin with a modified version of the msleep dataframe from ggplot2 that I've called msleep2. msleep2 contains the log10 transformed body and brain weights of 51 animals, which I've plotted as a scatterplot here.3. A new dataframe, with facets
Another variable, called vore contains information on eating behavior, which we can use to facet the plot. There are four eating habits: Herbivore, Carnivore, Insectivore and Omnivore.4. A new dataframe, with facets
To make sure you can read the labels we'll periodically zoom in, which means that the entire plot may not be shown.5. Poor labels and order
Two typical problems with facets are that they are often poorly labeled or that they have a wrong or inappropriate order.6. Poor labels and order
We can fix up some of the labels inside ggplot2, but some things are better done on the actual data before plotting. Let's take a look at both.7. The labeller argument
The facet layer functions have a labeller argument, which defaults to label_value.8. Using label_both adds the variable name
A more useful argument is to use label_both, which puts both the name of the variable and the level value.9. Two variables on one side
In the exercises, we saw that we can use multiple variables on rows or columns, like I've done here with the conservation status.10. Using label_context avoids ambiguity
In this case we can use the label_context argument which will add the variable name only if the labels may be ambiguous. If each variable has a small number of labels, this works well, as we'll see in the exercises. Unfortunately, this plot is already pretty overloaded and you can see that the labels are too long for the space given!11. Use rows and columns when appropriate
Let's switch to faceting on both rows and columns, in which case label_context defaults to label_value,12. Use rows and columns when appropriate
which means we can just leave it out.13. Use rows and columns when appropriate
Here, all the labels fit nicely inside the alloted space. Let's take a look at the actual facet labels and positions. To change these we need to go to the data set directly.14. Relabeling and reordering factors
The forcats package in the tidyverse collection contains some really useful functions here. I like fct_recode, as shown here, for relabeling level names in a factor variable.15. Reinitialize plot with new labels
If we reinitialize our plot, we'll see the new, informative, labels.16. Reinitialize plot with new labels
Nonetheless, we may still want them in a different order than alphabetical.17. Changing the order of levels
The fct_relevel function makes this really easy, as the second argument we just need to put the levels in our desired order.18. Reinitialize plot with new order
Now when we reinitialize out plot, our facets are in an appropriate order.19. Let's practice!
Let's try this out on a few exercises with the mtcars dataframe.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.