Challenge 7: Visualizing Debt Over Time

challenge_7
debt
Dirichi Umunna
Visualizing Multiple Dimensions
Author

Dirichi Umunna

Published

May 5, 2023

library(tidyverse)
library(ggplot2)
library(lubridate)
library(dplyr)


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

Read in Data

For this assignment, I will be working with the same dataset as in Challenge 7. This dataset contains quarterly measurements of household debt, specifically related to different types of loans. It serves as an extension of the previous challenge, allowing for further analysis and exploration of the data.

debtnew1 <- read_excel("_data/debt_in_trillions.xlsx")
Error in read_excel("_data/debt_in_trillions.xlsx"): could not find function "read_excel"
# add another date variable
debtnew1 <- debtnew1 %>%
  separate(`Year and Quarter`, into = c("Year", "Quarter"), sep = ":Q") %>%
  mutate(Year = paste0("20", Year)) %>%
  mutate(Year = as.numeric(Year))
Error in separate(., `Year and Quarter`, into = c("Year", "Quarter"), : object 'debtnew1' not found
# making a pivot longer dataset
  longerdebt <- debtnew1 %>%
  pivot_longer(cols = -c(Year, Total, Quarter), names_to = "Loan Type", values_to = "Debt")
Error in pivot_longer(., cols = -c(Year, Total, Quarter), names_to = "Loan Type", : object 'debtnew1' not found

Here, we read in the dataset and used the pivot longer function for ease of visualization.

Bivariate Analysis

##next, try to stack the data

ggplot(longerdebt, aes(x = Year, y = `Total`, fill = `Loan Type`)) +
  geom_bar(stat = "identity") +
  labs(x = "Year", y = " Total Debt Amount", fill = "Debt Type") +
  ggtitle("Stacked Bar Graph: Debt Types by Year") +
  scale_x_discrete(breaks = unique(longerdebt$Year))
Error in ggplot(longerdebt, aes(x = Year, y = Total, fill = `Loan Type`)): object 'longerdebt' not found

Here, we created a stacked bar showing the composition of different debt types by their respective debt amounts

Time Relationship

ggplot(longerdebt, aes(x = Year, y = `Total`, fill = `Loan Type`)) +
  geom_bar(stat = "identity") +
  labs(x = "Year", y = "Total Debt Amount", fill = "Debt Type") +
  ggtitle("Stacked Bar Graph: Debt Types by Year") +
  scale_x_continuous(breaks = seq(2000, 2025, by = 5)) +
  theme_minimal()
Error in ggplot(longerdebt, aes(x = Year, y = Total, fill = `Loan Type`)): object 'longerdebt' not found

Here, we utilize a scatter plot to examine the total debt over time. But what if we could show debt over time for each category of debt?

Multi-dimension Time Relationship

color_palette <- c("Mortgage" = "#FF0000",
                   "HE Revolving" = "#00FF00",
                   "Auto Loan" = "#0000FF",
                   "Credit Card" = "#FFFF00",
                   "Student Loan" = "#00FFFF",
                   "Other" = "#FF00FF")

ggplot(longerdebt, aes(x = Year, y = Total, color = `Loan Type`)) +
  geom_point(size = 6) +
  labs(x = "Year", y = "Debt Amount", color = "Debt Category") +
  ggtitle("Debt Over Time by Category") +
  theme_minimal() +
  scale_color_manual(values = color_palette)
Error in ggplot(longerdebt, aes(x = Year, y = Total, color = `Loan Type`)): object 'longerdebt' not found

In this analysis, I defined distinct color codes for each loan type and manually assigned them. I then created a multi-dimensional time relationship graph using a scatter plot. The scatter plot allowed us to visualize the changing patterns of debt over time for different loan types. Each loan type was represented by a unique color, making it easier to differentiate between the categories and observe their respective trends. I can only see one color here so I might have failed at doing something correctly.