# ggplot2 ggplot2::theme_set(ggplot2::theme_gray(base_size = 16)) # set seed set.seed(1234) # conflicted library(conflicted) conflict_prefer("filter", "dplyr") library(tidyverse) library(openintro) loans_full_schema <- loans_full_schema %>% mutate(grade = factor(grade, ordered = TRUE)) library(openintro) loans <- loans_full_schema %>% select(loan_amount, interest_rate, term, grade, state, annual_income, homeownership, debt_to_income) glimpse(loans) ggplot(loans, aes(x = homeownership)) + geom_bar() ggplot(loans, aes(x = homeownership)) + geom_bar(width=0.5, col="dodgerblue", fill="white") ggplot(loans, aes(x = factor(homeownership, levels=c("OWN", "MORTGAGE", "RENT")))) + geom_bar() + labs(x="homeownership") ggplot(loans, aes(x = homeownership)) + geom_bar() + scale_x_discrete(limits=c("OWN", "MORTGAGE", "RENT")) ggplot(loans, aes(x = homeownership, fill = grade)) + #<< geom_bar() ggplot(loans, aes(x = homeownership, fill = grade)) + geom_bar(position = "fill") #<< ggplot(loans, aes(x = homeownership, fill = grade)) + geom_bar() ggplot(loans, aes(x = homeownership, fill = grade)) + geom_bar(position = "fill") ggplot(loans, aes(x = homeownership, fill = grade)) + #<< geom_bar(position = "dodge") ggplot(loans, aes(y = homeownership, #<< fill = grade)) + geom_bar(position = "fill") + labs( #<< x = "Proportion", #<< y = "Homeownership", #<< fill = "Grade", #<< title = "Grades of Lending Club loans", #<< subtitle = "and homeownership of lendee" #<< ) #<< ggplot(loans, aes(x = loan_amount)) + geom_histogram(binwidth=5000) + facet_wrap(~ homeownership, nrow = 3) ggplot(loans, aes(x = loan_amount, col = homeownership, fill = homeownership)) + geom_histogram(binwidth=5000) + facet_wrap(~ homeownership, nrow = 3) ggplot(loans, aes(x = loan_amount, col = homeownership)) + geom_density(adjust=2, alpha=0.5) ggplot(loans, aes(x = loan_amount, col = homeownership, fill = homeownership)) + geom_density(adjust=2, alpha=0.5) ggplot(loans, aes(x = loan_amount, y = homeownership)) + geom_boxplot() ggplot(loans, aes(x = loan_amount, y = homeownership, col = homeownership)) + geom_boxplot() ggplot(loans, aes(x = homeownership, y = loan_amount)) + geom_violin() library(ggridges) ggplot(loans, aes(x = loan_amount, y = grade, fill = grade, color = grade)) + geom_density_ridges(alpha = 0.5) ggplot(loans, aes(x = loan_amount, fill = grade, color = grade)) + geom_density(alpha = 0.5) ggplot(loans, aes(x = loan_amount, y = grade, col = grade, fill = grade)) + geom_boxplot()