Challenge 6-debt

challenge_6
debt
Jinxia Niu
Visualizing Time and Relationships
Author

Jinxia Niu

Published

April 3, 2023

library(tidyverse)
library(ggplot2)

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 one graph including time (evolution)
  • try to make them “publication” ready (optional)
  • Explain why you choose the specific graph type
  1. Create at least one graph depicting part-whole or flow relationships
  • try to make them “publication” ready (optional)
  • 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.

  • debt ⭐
  • fed_rate ⭐⭐
  • abc_poll ⭐⭐⭐
  • usa_hh ⭐⭐⭐
  • hotel_bookings ⭐⭐⭐⭐
  • AB_NYC ⭐⭐⭐⭐⭐
install.packages("tidyverse")
library(tidyverse)
install.packages("readxl")
Error in contrib.url(repos, "source"): trying to use CRAN without setting a mirror
library(readxl)
df <- read_excel("_data/debt_in_trillions.xlsx")

Briefly describe the data

head(df, 10)
# A tibble: 10 × 8
   `Year and Quarter` Mortgage HE Revolvin…¹ Auto …² Credi…³ Stude…⁴ Other Total
   <chr>                 <dbl>         <dbl>   <dbl>   <dbl>   <dbl> <dbl> <dbl>
 1 03:Q1                  4.94         0.242   0.641   0.688   0.241 0.478  7.23
 2 03:Q2                  5.08         0.26    0.622   0.693   0.243 0.486  7.38
 3 03:Q3                  5.18         0.269   0.684   0.693   0.249 0.477  7.56
 4 03:Q4                  5.66         0.302   0.704   0.698   0.253 0.449  8.07
 5 04:Q1                  5.84         0.328   0.72    0.695   0.260 0.446  8.29
 6 04:Q2                  5.97         0.367   0.743   0.697   0.263 0.423  8.46
 7 04:Q3                  6.21         0.426   0.751   0.706   0.33  0.41   8.83
 8 04:Q4                  6.36         0.468   0.728   0.717   0.346 0.423  9.04
 9 05:Q1                  6.51         0.502   0.725   0.71    0.364 0.394  9.21
10 05:Q2                  6.70         0.528   0.774   0.717   0.374 0.402  9.49
# … with abbreviated variable names ¹​`HE Revolving`, ²​`Auto Loan`,
#   ³​`Credit Card`, ⁴​`Student Loan`
summary(df)
 Year and Quarter      Mortgage       HE Revolving      Auto Loan     
 Length:74          Min.   : 4.942   Min.   :0.2420   Min.   :0.6220  
 Class :character   1st Qu.: 8.036   1st Qu.:0.4275   1st Qu.:0.7430  
 Mode  :character   Median : 8.412   Median :0.5165   Median :0.8145  
                    Mean   : 8.274   Mean   :0.5161   Mean   :0.9309  
                    3rd Qu.: 9.047   3rd Qu.:0.6172   3rd Qu.:1.1515  
                    Max.   :10.442   Max.   :0.7140   Max.   :1.4150  
  Credit Card      Student Loan        Other            Total       
 Min.   :0.6590   Min.   :0.2407   Min.   :0.2960   Min.   : 7.231  
 1st Qu.:0.6966   1st Qu.:0.5333   1st Qu.:0.3414   1st Qu.:11.311  
 Median :0.7375   Median :0.9088   Median :0.3921   Median :11.852  
 Mean   :0.7565   Mean   :0.9189   Mean   :0.3831   Mean   :11.779  
 3rd Qu.:0.8165   3rd Qu.:1.3022   3rd Qu.:0.4154   3rd Qu.:12.674  
 Max.   :0.9270   Max.   :1.5840   Max.   :0.4860   Max.   :14.957  
glimpse(df)
Rows: 74
Columns: 8
$ `Year and Quarter` <chr> "03:Q1", "03:Q2", "03:Q3", "03:Q4", "04:Q1", "04:Q2…
$ Mortgage           <dbl> 4.942, 5.080, 5.183, 5.660, 5.840, 5.967, 6.210, 6.…
$ `HE Revolving`     <dbl> 0.242, 0.260, 0.269, 0.302, 0.328, 0.367, 0.426, 0.…
$ `Auto Loan`        <dbl> 0.641, 0.622, 0.684, 0.704, 0.720, 0.743, 0.751, 0.…
$ `Credit Card`      <dbl> 0.688, 0.693, 0.693, 0.698, 0.695, 0.697, 0.706, 0.…
$ `Student Loan`     <dbl> 0.2407000, 0.2429000, 0.2488000, 0.2529000, 0.25980…
$ Other              <dbl> 0.4776, 0.4860, 0.4773, 0.4486, 0.4465, 0.4231, 0.4…
$ Total              <dbl> 7.2313, 7.3839, 7.5551, 8.0655, 8.2893, 8.4600, 8.8…
# The data includes 74 rows and 8 variables, includes quarterly measures of the total amount of household debt associated with 6 different types of loans - mortgage,HE revolving, auto, credit card, student, and other - plus a total household debt including all 6 loan types. 

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.

debt <- df %>%
  mutate(date = parse_date_time(`Year and Quarter`, 
                           orders="yq")) %>%
  select(date,everything())
  print(debt)
# A tibble: 74 × 9
   date                Year and …¹ Mortg…² HE Re…³ Auto …⁴ Credi…⁵ Stude…⁶ Other
   <dttm>              <chr>         <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <dbl>
 1 2003-01-01 00:00:00 03:Q1          4.94   0.242   0.641   0.688   0.241 0.478
 2 2003-04-01 00:00:00 03:Q2          5.08   0.26    0.622   0.693   0.243 0.486
 3 2003-07-01 00:00:00 03:Q3          5.18   0.269   0.684   0.693   0.249 0.477
 4 2003-10-01 00:00:00 03:Q4          5.66   0.302   0.704   0.698   0.253 0.449
 5 2004-01-01 00:00:00 04:Q1          5.84   0.328   0.72    0.695   0.260 0.446
 6 2004-04-01 00:00:00 04:Q2          5.97   0.367   0.743   0.697   0.263 0.423
 7 2004-07-01 00:00:00 04:Q3          6.21   0.426   0.751   0.706   0.33  0.41 
 8 2004-10-01 00:00:00 04:Q4          6.36   0.468   0.728   0.717   0.346 0.423
 9 2005-01-01 00:00:00 05:Q1          6.51   0.502   0.725   0.71    0.364 0.394
10 2005-04-01 00:00:00 05:Q2          6.70   0.528   0.774   0.717   0.374 0.402
# … with 64 more rows, 1 more variable: Total <dbl>, and abbreviated variable
#   names ¹​`Year and Quarter`, ²​Mortgage, ³​`HE Revolving`, ⁴​`Auto Loan`,
#   ⁵​`Credit Card`, ⁶​`Student Loan`

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?

Time Dependent Visualization

library(ggplot2)
ggplot(debt, aes(date,Total))+geom_point()

ggplot(debt,aes(date,Mortgage))+geom_line()

Visualizing Part-Whole Relationships

debt_long<-debt%>%
  pivot_longer(cols = Mortgage:Other,
               names_to = "Loan", 
               values_to = "total")%>%
  select(-Total)%>%
  mutate(Loan = as.factor(Loan))

ggplot(debt_long, aes(x=date, y=total, color=Loan)) +
  geom_point(size=.5) +
  geom_line() +
  theme(legend.position = "right")