Get access to all lessons in this course.
Going Deeper with R
Advanced Data Wrangling and Analysis
- Importing Data
- Tidy Data
- Reshaping Data
- Dealing with Missing Data
- Changing Variable Types
- Advanced Variable Creation
- Advanced Summarizing
- Binding Data Frames
- Merging Data
- Renaming Variables
- Quick Interlude to Reorganize our Code
- Exporting Data
Advanced Data Visualization
- Data Visualization Best Practices
- Tidy Data
- Pipe Data Into ggplot
- Reorder Plots to Highlight Findings
- Line Charts
- Use Color to Highlight Findings
- Use the scales Package for Nicely Formatted Values
- Use Direct Labeling
- Use Axis Text Wisely
- Use Titles to Highlight Findings
- Use Color in Titles to Highlight Findings
- Use Annotations to Explain
- Tweak Spacing
- Customize Your Theme
- Customize Your Fonts
- Try New Plot Types
- Advanced Markdown Text Formatting
- Advanced YAML
- Inline R Code
- Making Your Reports Shine: Word Edition
- Making Your Reports Shine: HTML Edition
- Making Your Reports Shine: PDF Edition
- Other Formats
- You Did It!
Reorder Plots to Highlight Findings
This lesson is locked
This lesson is called Reorder Plots to Highlight Findings, part of the Going Deeper with R course. This lesson is called Reorder Plots to Highlight Findings, part of the Going Deeper with R course.
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
# reorder() solution enrollment_by_race_ethnicity %>% filter(district == "Beaverton SD 48J") %>% filter(year == "2018-2019") %>% ggplot(aes(x = percent_of_total_at_school, y = reorder(race_ethnicity, percent_of_total_at_school))) + geom_col() # fct_reorder() solution enrollment_by_race_ethnicity %>% filter(district == "Beaverton SD 48J") %>% filter(year == "2018-2019") %>% mutate(race_ethnicity = fct_reorder(race_ethnicity, percent_of_total_at_school)) %>% ggplot(aes(x = percent_of_total_at_school, y = race_ethnicity)) + geom_col()
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:
reorder()function, make a bar chart that shows the percent of race/ethnicity groups in descending order
Make the same bar chart using
fct_reorder()to reorder the race/ethnicity groups
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
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.