Challenge 5 Instructions

challenge_5
railroads
cereal
air_bnb
pathogen_cost
australian_marriage
public_schools
usa_households
Introduction to Visualization
Author

Kevin Martell Luya

Published

April 23, 2023

library(tidyverse)
library(ggplot2)
library(readxl)
library(lattice)

knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)

Challenge Overview

Today’s challenge is to:

  1. read in a data set, and describe the data set using both words and any supporting information (e.g., tables, etc)
  2. tidy data (as needed, including sanity checks)
  3. mutate variables as needed (including sanity checks)
  4. create at least two univariate visualizations
  • try to make them “publication” ready
  • Explain why you choose the specific graph type
  1. Create at least one bivariate visualization
  • try to make them “publication” ready
  • Explain why you choose the specific graph type

R Graph Gallery is a good starting point for thinking about what information is conveyed in standard graph types, and includes example R code.

(be sure to only include the category tags for the data you use!)

Read in data

Read in one (or more) of the following datasets, using the correct R package and command.

  • cereal.csv ⭐
  • Total_cost_for_top_15_pathogens_2018.xlsx ⭐
  • Australian Marriage ⭐⭐
  • AB_NYC_2019.csv ⭐⭐⭐
  • StateCounty2012.xls ⭐⭐⭐
  • Public School Characteristics ⭐⭐⭐⭐
  • USA Households ⭐⭐⭐⭐⭐

Briefly describe the data

Tidy Data (as needed)

Is your data already tidy, or is there work to be done? Be sure to anticipate your end result to provide a sanity check, and document your work here.

data <-read_xls("./_data/StateCounty2012.xls",
                skip = 4,
                col_names= c("STATE", "_trash",  "COUNTY",
                          "_trash", "EMPLOYEES"))%>%
  select(!contains("_trash"))%>%
  filter(!str_detect(STATE, "Total"))

data<-head(data, -2)%>%
  mutate(COUNTY = ifelse(STATE=="CANADA", "CANADA", COUNTY))

data
# A tibble: 2,931 × 3
   STATE COUNTY               EMPLOYEES
   <chr> <chr>                    <dbl>
 1 AE    APO                          2
 2 AK    ANCHORAGE                    7
 3 AK    FAIRBANKS NORTH STAR         2
 4 AK    JUNEAU                       3
 5 AK    MATANUSKA-SUSITNA            2
 6 AK    SITKA                        1
 7 AK    SKAGWAY MUNICIPALITY        88
 8 AL    AUTAUGA                    102
 9 AL    BALDWIN                    143
10 AL    BARBOUR                      1
# … with 2,921 more rows

Are there any variables that require mutation to be usable in your analysis stream? For example, do you need to calculate new values in order to graph them? Can string values be represented numerically? Do you need to turn any variables into factors and reorder for ease of graphics and visualization?

Document your work here.

railroad_summaries <- data %>%
  group_by(STATE) %>%
  summarise("state_employees_average" = round(sum(EMPLOYEES)/ n_distinct(COUNTY)),
         "state_counties"= n_distinct(COUNTY)) 
railroad_summaries
# A tibble: 54 × 3
   STATE  state_employees_average state_counties
   <chr>                    <dbl>          <int>
 1 AE                           2              1
 2 AK                          17              6
 3 AL                          64             67
 4 AP                           1              1
 5 AR                          54             72
 6 AZ                         210             15
 7 CA                         239             55
 8 CANADA                     662              1
 9 CO                          64             57
10 CT                         324              8
# … with 44 more rows

Univariate Visualizations

ggplot(railroad_summaries, aes(state_employees_average))+
  geom_histogram()

Bivariate Visualization(s)

ggplot(railroad_summaries, aes(state_counties,state_employees_average))+
  geom_point()

railroad_summaries %>% 
  arrange(desc(state_employees_average)) %>%
  slice(1:15) %>%
  ggplot(aes(reorder(STATE,-state_employees_average),state_employees_average))+
  geom_col(fill="green")+
  labs(x="STATE")

railroad_summaries %>% 
  arrange((state_employees_average)) %>%
  slice(1:15) %>%
  ggplot(aes(reorder(STATE,-state_employees_average),state_employees_average))+
  geom_col(fill="green")+
  labs(x="STATE")