Reorder Plots to Highlight Findings
This lesson is called Reorder Plots to Highlight Findings, part of the R in 3 Months (Fall 2022) course. This lesson is called Reorder Plots to Highlight Findings, part of the R in 3 Months (Fall 2022) course.
Transcript
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
Your Turn
Make a bar chart that shows race/ethnicity in Beaverton SD 48J. As before, filter your data to only include 2018-2019 data and only include Beaverton SD 48J. Then, do the following:
Using the
reorder()
function, make a bar chart that shows the percent of race/ethnicity groups in descending orderMake the same bar chart using
mutate()
andfct_reorder()
to reorder the race/ethnicity groups
Learn More
I mention that the fct_reorder()
function comes from the forcats
package in the video. Here’s how the documentation lays out the goal of forcats
:
R uses factors to handle categorical variables, variables that have a fixed and known set of possible values. Factors are also helpful for reordering character vectors to improve display. The goal of the forcats package is to provide a suite of tools that solve common problems with factors, including changing the order of levels or the values.
As the tweet below demonstrates, using factors can be complicated.
making any bar graphs with factors/categories is still my least favorite thing in the world pic.twitter.com/Neg8qp9ueq
— Damie Pak (@pakdamie) May 12, 2020
If you want to dig into factors a bit more, start with Chapter 15 of R for Data Science and Chapter 10 of Stat 545.
I'd also suggest reading the 2017 article Wrangling Categorical Data in R by Amelia McNamara and Nicholas Horton. Amelia McNamara also gave a talk in 2019 at rstudio::conf about factors , which is very helpful.
Have any questions? Put them below and we will help you out!
Course Content
142 Lessons
You need to be signed-in to comment on this post. Login.
Alberto Espinoza • October 7, 2022
David, are there instances where you prefer to use reorder() vs. fct_reorder() or vice-versa? I understand both concepts but not sure in which situations to employ either. Or is it a matter of preference?