library(openintro) library(tidyverse) library(dplyr) library(tidyr)
data(“loans_full_schema”) ?loans_full_schema
loans <- loans_full_schema %>% mutate(application_type = as.character(application_type)) # [2]
loans %>% count(application_type, homeownership) %>% pivot_wider(names_from = homeownership, values_from = n) # [2], [3]
ggplot(loans, aes(x = homeownership)) + geom_bar(fill = “#1a4e5c”) + labs(title = “Counts of Homeownership”, x = “Homeownership”, y = “Count”) # [3]
ggplot(loans, aes(x = homeownership, y = after_stat(count / sum(count)))) + geom_bar(fill = “#1a4e5c”) + labs(title = “Proportions of Homeownership”, x = “Homeownership”, y = “Proportion”) # [3], [4]
ggplot(loans, aes(x = homeownership, fill = application_type)) + geom_bar() + labs(title = “Stacked Bar Plot”) # [5]
ggplot(loans, aes(x = homeownership, fill = application_type)) + geom_bar(position = “fill”) + labs(title = “Standardized Bar Plot”, y = “Proportion”) # [5]
ggplot(loans, aes(x = homeownership, fill = application_type)) + geom_bar(position = “dodge”) + labs(title = “Dodged Bar Plot”) # [5]
install.packages(“vcd”) library(vcd) mosaic(application_type ~ homeownership, data = loans) # [8]
loans %>% count(application_type, homeownership) %>% group_by(application_type) %>% mutate(proportion = n / sum(n)) # [11]
loans %>% count(application_type, homeownership) %>% group_by(homeownership) %>% mutate(proportion = n / sum(n)) # [10]
ggplot(loans, aes(x = ““, fill = homeownership)) + geom_bar(width = 1) + coord_polar(”y”) + theme_void() # [12]
install.packages(“waffle”) library(waffle) waffle_data <- table(loans$homeownership) / 100 # Representing proportions waffle(waffle_data) # [13], [14]
library(usdata) county_data <- county %>% filter(!is.na(pop_change)) %>% mutate(change_type = ifelse(pop_change > 0, “gain”, “no gain”)) # [15]
ggplot(county_data, aes(x = median_hh_income, fill = change_type)) + geom_histogram(alpha = 0.5, position = “identity”) # [17], [18]
ggplot(county_data, aes(x = median_hh_income, y = change_type)) + geom_boxplot() # [18]
install.packages(“ggridges”) library(ggridges) ggplot(county_data, aes(x = median_hh_income, y = change_type)) + geom_density_ridges() # [19]
ggplot(county_data, aes(x = median_hh_income)) + geom_histogram() + facet_grid(change_type ~ metro) # [20], [21]