Skip to content
Mapping with R has been completely revamped. Check it out →
R for the Rest of Us Logo

Going Deeper with R

Reorder Plots to Highlight Findings

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.

View code shown in video
# Load Packages -----------------------------------------------------------

library(tidyverse)
library(fs)

# Create Directory --------------------------------------------------------

dir_create("data")

# Download Data -----------------------------------------------------------

# download.file("https://github.com/rfortherestofus/going-deeper-v2/raw/main/data/third_grade_math_proficiency.rds",
#               mode = "wb",
#               destfile = "data/third_grade_math_proficiency.rds")

# Import Data -------------------------------------------------------------

third_grade_math_proficiency <- 
  read_rds("data/third_grade_math_proficiency.rds") |> 
  select(academic_year, school, school_id, district, proficiency_level, number_of_students) |> 
  mutate(is_proficient = case_when(
    proficiency_level >= 3 ~ TRUE,
    .default = FALSE
  )) |> 
  group_by(academic_year, school, district, school_id, is_proficient) |> 
  summarize(number_of_students = sum(number_of_students, na.rm = TRUE)) |> 
  ungroup() |> 
  group_by(academic_year, school, district, school_id) |> 
  mutate(percent_proficient = number_of_students / sum(number_of_students, na.rm = TRUE)) |> 
  ungroup() |> 
  filter(is_proficient == TRUE) |> 
  select(academic_year, school, district, percent_proficient) |> 
  rename(year = academic_year) |> 
  mutate(percent_proficient = case_when(
    is.nan(percent_proficient) ~ NA,
    .default = percent_proficient
  ))

# Plot --------------------------------------------------------------------

third_grade_math_proficiency |> 
  filter(year == "2021-2022") |> 
  filter(district == "Portland SD 1J") |> 
  ggplot(aes(x = percent_proficient, 
             y = school)) +
  geom_col()

third_grade_math_proficiency |> 
  filter(year == "2021-2022") |> 
  filter(district == "Portland SD 1J") |> 
  ggplot(aes(x = percent_proficient, 
             y = reorder(school, percent_proficient))) +
  geom_col()

third_grade_math_proficiency |> 
  filter(year == "2021-2022") |> 
  filter(district == "Portland SD 1J") |> 
  mutate(school = fct_reorder(school, percent_proficient)) |> 
  ggplot(aes(x = percent_proficient, 
             y = school)) +
  geom_col()

Your Turn

  1. Make a bar chart that shows race/ethnicity in Beaverton SD 48J. As before, filter your data to only include 2022-2023 data and only include Beaverton SD 48J. Then, do the following:

  2. Using the reorder() function, make a bar chart that shows the percent of race/ethnicity groups in descending order

  3. Make the same bar chart using mutate() and fct_reorder() to reorder the race/ethnicity groups

Learn More

R for the Rest of Us consultant Albert Rapp has written a blog post on reordering items in plots.

If you want to delve deeper into factors, check out:

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Myles Kwitny

Myles Kwitny • May 20, 2025

When I ran my code, I did the select and filter functions during the creation of my data set instead of during the plotting stage. Is this an okay way to do it or could it cause problems if I was doing more complex data plotting? Below is the code I used for reference:

third_grade_math_proficiency_by_race_2022_2023 <- read_rds("data/enrollment_by_race_ethnicity.rds") |> select(year, district, number_of_students,race_ethnicity, pct) |> filter(district == "Beaverton SD 48J", year == "School 2022-23")

third_grade_math_proficiency_by_race_2022_2023 |> ggplot(aes(x = pct, y = reorder(race_ethnicity, pct))) + geom_col()

Gracielle Higino

Gracielle Higino Coach • May 20, 2025

Hi Myles! That's not a problem as long as you have descriptive object names, as you do in your example! However, if you want to do that for several years, you'll end up with a bunch of objects on your environment and this can make your computer slower depending on how big your datasets are. Just keep in mind that the method that David is teaching here can be useful in the future when you're learning about parameterized reports, or when you put that into a function.

Myles Kwitny

Myles Kwitny • May 21, 2025

Hi Gracielle,

That makes sense! Thank you!

Course Content

44 Lessons