library(openintro)
## 载入需要的程序包:airports
## 载入需要的程序包:cherryblossom
## 载入需要的程序包:usdata
library(tidyverse)
## -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
## v dplyr     1.1.4     v readr     2.1.6
## v forcats   1.0.1     v stringr   1.6.0
## v ggplot2   4.0.1     v tibble    3.3.1
## v lubridate 1.9.4     v tidyr     1.3.2
## v purrr     1.2.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
  1. How does loan interest rate vary by borrower credit grade?
ggplot(loans_full_schema, aes(x = grade, y = interest_rate)) +
  geom_boxplot() +
  labs(title = "Interest Rate by Credit Grade",
       x = "Credit Grade",
       y = "Interest Rate (%)")

  1. Within each grade, does debt-to-income ratio affect interest rates?

ggplot(loans_full_schema,
       aes(x = debt_to_income, y = interest_rate, color = grade)) +
  geom_point(alpha = 0.4) +
  labs(title = "Interest Rate vs Debt-to-Income Ratio",
       x = "Debt-to-Income Ratio",
       y = "Interest Rate (%)")
## Warning: Removed 24 rows containing missing values or values outside the scale range
## (`geom_point()`).

  1. Does the loan term affect the interest rate?

ggplot(loans_full_schema, aes(x = term, y = interest_rate)) +
  geom_boxplot() +
  labs(title = "Interest Rate by Loan Term",
       x = "Loan Term",
       y = "Interest Rate")
## Warning: Orientation is not uniquely specified when both the x and y aesthetics are
## continuous. Picking default orientation 'x'.
## Warning: Continuous x aesthetic
## i did you forget `aes(group = ...)`?

  1. Are long-term loans concentrated in certain credit ratings?

ggplot(loans_full_schema, aes(x = term, fill = grade)) +
  geom_bar(position = "fill") +
  labs(title = "Distribution of Grades within Loan Terms",
       y = "Proportion")