Skip to main content

Section 9.5 Visualize Relationships

We are going to make three different visualizations:
  1. A bar plot of defaults
  2. A histogram of balances split by default, and then
  3. A scatterplot of balance and income
For fun, we will be utilizing some themes from the ggthemes package. When recreating these on your own, of course feel free to play around and change the themes. As I always say, R puts the r in Artist!
library(ggthemes)

# Default distribution
ggplot(cc_data, aes(x = default, fill = default)) +
  geom_bar() +
  labs(title = "Count of Defaulters vs Non-Defaulters", y = "Count", x = "Default?") +
  theme_economist()
A bar chart showing the count of No defaults (9,677) and Yes defaults (323). The No bar is much taller, confirming that defaults are rare in the dataset.
Figure 9.5.1. Most customers do not default on their credit cards, with defaulters representing only about 3% of the dataset.
For a quick review on histograms, see SubsubsectionΒ 3.4.4.1.
# Balance by default
ggplot(cc_data, aes(x = balance, fill = default)) +
  geom_histogram(position = "identity", alpha = 0.6, bins = 30) +
  labs(title = "Higher Balances Increase Likelihood of Default",
       x = "Average Credit Card Balance ($)", y = "Count") +
  theme_economist_white()
An overlapping histogram showing credit card balance distributions for defaulters (Yes, red) and non-defaulters (No, teal). Non-defaulters cluster at lower balances while defaulters show a distribution shifted toward higher balances.
Figure 9.5.2. While most customers do not default on their credit cards, it seems that people who do have higher balances.
# Balance vs. Income
ggplot(cc_data, aes(x = balance, y = income, color = default)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm", se = FALSE, color = "black", linetype = "dashed") +
  labs(title = "Customers Who Default Tend to Have Higher Balances",
       subtitle = "Each point represents a customer",
       x = "Average Credit Card Balance ($)",
       y = "Annual Income ($)",
       color = "Default") +
  theme_minimal(base_size = 13) +
  theme(plot.title = element_text(face = "bold"))
A scatterplot with credit card balance on the x-axis and annual income on the y-axis. Points are colored by default status. A dashed trend line shows the overall negative association between balance and income.
Figure 9.5.3. Regardless of if you default or not, there is a negative relationship between Annual Income and Average Credit Card Balance.
The visualizations look great! In terms of insights, we see (again) that most people do not default on their credit cards, most balances are around the 800 range for non-defaulters and higher for the defaulters, and that as balance increases, income decreases.
With a basic understanding of our data, it is now time to do something really fun - split our data!