Flipping factors: creating a sorted bar chart

Factors are not always as fun as you think they're going to be

By Jesse Mostipak in tutorial

June 29, 2021

Y’all.

Just when I think I’ve wrapped my head around factors a #TidyTuesday dataset drops that forces me to stare into space and question whether or not I even know how to code. Thankfully Twitch chat was there for me, and because our community is so awesome I have a walkthrough of the code we used to create a sorted bar chart showing the total number of animals rescued in London from 2009–2021. There’s a lot of background knowledge required to grok what’s happening with our wrangling code. If this looks unfamiliar don’t sweat it too much – I’m working on getting out some highlight videos that will go more in-depth with the wrangling steps.

Watch the walkthrough

Set up our environment

You don’t necessarily need the {here} package for this walk-through. I use it at the very end when I save the plot that we create, but if you aren’t coding within an RStudio Project you can leave it out.

library(tidyverse)
library(here)

Import and inspect our data

I copied and pasted the data from the Week 27 page in the #TidyTuesday GitHub repo.

animal_rescues <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-06-29/animal_rescues.csv', guess_max = 7544)

glimpse(animal_rescues)

Wrangling our data

The goals of this wrangling step are to:

  • correct the typo and put cat and Cat in the same category
  • list Budgie and Pigeon data as Bird
  • list all of the rows that contain the phrase “Unknown - *” as Unknown
  • convert all of our character data to factors
data <- animal_rescues %>% 
  mutate(animal_group_parent = recode(animal_group_parent, 
                                      cat = "Cat",
                                      Budgie = "Bird",
                                      Pigeon = "Bird"),
         animal_group_parent = case_when(str_detect(animal_group_parent, "Unknown") ~ "Unknown", 
                                         TRUE ~ animal_group_parent)) %>% 
  mutate(across(where(is.character), factor))

glimpse(data)

Create our sorted bar chart

data %>% 
  count(animal_group_parent) %>% 
  ggplot(aes(reorder(animal_group_parent, n), n, fill = animal_group_parent)) +
  geom_col(show.legend = FALSE) + 
  coord_flip() 

Save our plot

ggsave(here("06_animal_rescues", "sorted_bar.png"))

Next steps

As always this plot is meant to get you started on your #TidyTuesday submission for this week, and some natural extensions of what we created are to:

  • refine the axis labels and add a title
  • use a custom color palette and//or apply a new theme
  • explore a specific species
  • look at how counts change by year
Posted on:
June 29, 2021
Length:
2 minute read, 356 words
Categories:
tutorial
Tags:
TidyTuesday bar chart ggplot dataviz
See Also:
Creating a bump plot using {ggbump} and Parks Access data
Stream makes a streamplot