#nyc_ratio_perloan <- read_dta('~/NYC extract with zcta and codes and ratios.dta')
nyc_ratio_perzcta <- read_dta('~/NYC CBSA ZCTA data ordered.dta')

nyc_ratio_perzcta_select <- nyc_ratio_perzcta %>%
  select(zcta, loan_notsusp_tobizratio, MAJMIN, POV20noCollTract, POP, ZCTAinNYC)
options(tigris_use_cache = TRUE)

nyc_tidycensus_pull <- get_acs(
  state = "NY",
  geography = "zcta",
  variables = "B00001_001",
  geometry = TRUE,
  year = 2018
)

# grab only the relevant zctas...
nyc_tidycensus_filt <- nyc_tidycensus_pull %>%
  filter(GEOID %in% nyc_ratio_perzcta$zcta)

nyc_map_loan_merge <- nyc_tidycensus_filt %>%
  left_join(nyc_ratio_perzcta_select, by = c("GEOID" = "zcta"))

#nyc_map_loan_merge$loan_notsusp_tobizratio
nyc_map_ready <- nyc_map_loan_merge %>%
  filter(POP != 0 & loan_notsusp_tobizratio < 2 & ZCTAinNYC == 1) %>%
  mutate(loan_ratio_cat = case_when(loan_notsusp_tobizratio < 0.54 ~ "Under 0.54",
                                    loan_notsusp_tobizratio >= 0.54 & loan_notsusp_tobizratio < 0.72 ~ "0.54 - 0.71",
                                    loan_notsusp_tobizratio >= 0.72 & loan_notsusp_tobizratio < 0.9 ~ "0.72 - 0.9",
                                    loan_notsusp_tobizratio >= 0.9 ~ "Above 0.9"),
         loan_ratio_cat = fct_relevel(loan_ratio_cat, "Under 0.54", "0.54 - 0.71", "0.72 - 0.9", "Above 0.9")) %>%
  mutate(demographic_splits = case_when(MAJMIN == 1 & POV20noCollTract == 1 ~ "Minority & High Poverty",
                                        MAJMIN == 1 & POV20noCollTract == 0 ~ "Minority & Low Poverty",
                                        MAJMIN == 0 ~ "White"))

Overall Loan Ratios

nyc_map_ready %>%
  ggplot(aes(fill = loan_ratio_cat)) + 
  geom_sf(color = "black", size = .2) + 
  scale_fill_brewer(type = "seq", palette = 2, direction = 1) +
  robins_ggplot_theme() +
  labs(fill = "Loans to Businesses",
       title = "PPP Loan Ratios by ZCTA in New York City",
       subtitle = "Data Obtained from the US Census, Griffin et. al (2022), and the SBA\n")

library(RColorBrewer)
library(leaflet)
#brewer.pal(name="BuGn", n=4)
pal_gen_ratios <- colorFactor(palette = c("#238B45", "#66C2A4", "#B2E2E2", "#EDF8FB"), 
                        domain = nyc_map_ready$loan_ratio_cat, 
                        reverse = TRUE)

gen_ratios_content <- paste("<b>", nyc_map_ready$GEOID, 
                 "</b></br><em>", "Loans to Businesses:",
                 "</em>", round(nyc_map_ready$loan_notsusp_tobizratio, digits = 3),
                 "</br><em>", "Demographics:",
                 "</em>", nyc_map_ready$demographic_splits)

nyc_map_ready %>%
  leaflet(options = leafletOptions(minZoom = 5, maxZoom = 12)) %>%
  addPolygons(popup = ~gen_ratios_content, fillColor = ~pal_gen_ratios(loan_ratio_cat),
              stroke = TRUE, fillOpacity = 0.9, color = "black",
              opacity = 1, weight = 1.5) %>%
  addLegend("bottomright", pal = pal_gen_ratios, 
            values = ~loan_ratio_cat, 
            title = "Loans to Businesses",
            opacity = 1)

Faceted Loan Ratios by Race & Poverty

nyc_map_ready %>%
  ggplot(aes(fill = loan_ratio_cat)) + 
  geom_sf(color = "black", size = .2) + 
  facet_wrap(~demographic_splits, ncol = 2) +
  scale_fill_brewer(type = "seq", palette = 2, direction = 1) +
  robins_facet_theme() +
  labs(fill = "Loans to Businesses",
       title = "PPP Loan Ratios by ZCTA and Demographics in New York City",
       subtitle = "Data Obtained from the US Census, Griffin et. al (2022), and the SBA\n")

Chloropleth Loan Ratios by Race & Poverty

nyc_map_ready %>%
  ggplot(aes(fill = demographic_splits, alpha = loan_ratio_cat)) + 
  geom_sf(color = "black", size = .2) + 
  scale_fill_brewer(type = "qual", palette = 6) +
  robins_ggplot_theme() +
  labs(fill = "Demographics",
       title = "PPP Loan Ratios by ZCTA and Demographics in New York City",
       subtitle = "Data Obtained from the US Census, Griffin et. al (2022), and the SBA\n",
       alpha = "Loans to Businesses")

#brewer.pal(name="Set1", n=3)

pal_dem_ratios <- colorFactor(palette = c("#E41A1C", "#377EB8", "#4DAF4A"), 
                        domain = nyc_map_ready$demographic_splits)

dem_ratios_content <- paste("<b>", nyc_map_ready$GEOID, 
                 "</b></br><em>", "Loans to Businesses:",
                 "</em>", round(nyc_map_ready$loan_notsusp_tobizratio, digits = 3),
                 "</br><em>", "Demographics:",
                 "</em>", nyc_map_ready$demographic_splits)

nyc_map_ready %>%
  mutate(loan_ratio_cat = case_when(loan_ratio_cat == "Under 0.54" ~ 0.25,
                             loan_ratio_cat == "0.54 - 0.71" ~ 0.5,
                             loan_ratio_cat == "0.72 - 0.9" ~ 0.8,
                             loan_ratio_cat == "Above 0.9" ~ 1)) %>%
  leaflet(options = leafletOptions(minZoom = 5, maxZoom = 12)) %>%
  addPolygons(popup = ~dem_ratios_content, fillColor = ~pal_dem_ratios(demographic_splits),
              stroke = TRUE, fillOpacity = ~loan_ratio_cat, color = "black",
              opacity = 1, weight = 1.5) %>%
  addLegend("bottomright", pal = pal_dem_ratios, 
            values = ~demographic_splits, 
            title = "Demographic Splits",
            opacity = 1)