library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
library(ggplot2)

df<- fread("C:/Users/desib/OneDrive/Documents/URP 5363/Oct 1 Lesson/CA_MSA.csv")

HOLC<- fread("C:/Users/desib/OneDrive/Documents/URP 5363/Oct 1 Lesson/holc_census_tracts.csv")
df%>% group_by(NAME) %>% summarize(diss_b_w=sum(wa))
## # A tibble: 6 × 2
##   NAME                                 diss_b_w
##   <chr>                                   <dbl>
## 1 Fresno, CA                              0.755
## 2 Los Angeles-Long Beach-Anaheim, CA      0.952
## 3 Riverside-San Bernardino-Ontario, CA    0.842
## 4 San Diego-Chula Vista-Carlsbad, CA      0.960
## 5 San Francisco-Oakland-Berkeley, CA      0.911
## 6 San Jose-Sunnyvale-Santa Clara, CA      0.855
HOLC %>% group_by(st_name ) %>% summarize(avg=mean(holc_area))
## # A tibble: 169 × 2
##    st_name         avg
##    <chr>         <dbl>
##  1 Akron         1.50 
##  2 Albany        0.619
##  3 Altoona       0.428
##  4 Asheville     1.26 
##  5 Atlanta       0.587
##  6 Atlantic City 0.683
##  7 Augusta       0.448
##  8 Aurora        0.644
##  9 Austin        1.99 
## 10 Baltimore     1.56 
## # ℹ 159 more rows
ggplot(HOLC, aes(x=holc_area))+ geom_boxplot()

HOLCD<-HOLC  %>%
  filter(state == "TX" & holc_grade =="D") %>%
  group_by(st_name) %>%
  summarize(grade_D_count=n())
library(tidycensus)

var <- c(poptotal='B03002_001E', 
         hispanic='B03002_012E',
         white='B03002_003E',
         black='B03002_004E', 
         asian='B03002_006E',
         poptotal2='B17017_001E',
         poverty='B17017_002E') 


st <-"TX"
ct <-"Bexar"

ct <- get_acs(geography = "tract", variables = var, count=ct,
               state = st,output="wide", year = 2021, geometry = TRUE)
## Getting data from the 2017-2021 5-year ACS
## Warning: • You have not set a Census API key. Users without a key are limited to 500
## queries per day and may experience performance limitations.
## ℹ For best results, get a Census API key at
## http://api.census.gov/data/key_signup.html and then supply the key to the
## `census_api_key()` function to use it throughout your tidycensus session.
## This warning is displayed once per session.
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
##   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |==========================                                            |  37%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  40%  |                                                                              |============================                                          |  41%  |                                                                              |=============================                                         |  41%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  42%  |                                                                              |==========================================                            |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |==========================================                            |  61%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  62%  |                                                                              |============================================                          |  63%  |                                                                              |============================================                          |  64%  |                                                                              |=============================================                         |  64%  |                                                                              |=============================================                         |  65%  |                                                                              |==============================================                        |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |================================================                      |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |=================================================                     |  71%  |                                                                              |==================================================                    |  71%  |                                                                              |==================================================                    |  72%  |                                                                              |===================================================                   |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  74%  |                                                                              |====================================================                  |  75%  |                                                                              |=====================================================                 |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  77%  |                                                                              |======================================================                |  78%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |======================================================================| 100%
ct$black_pct <-ct$black/ct$poptotal
ct$white_pct <- ct$white/ct$poptotal
ct$hispanic_pct <- ct$hispanic/ct$poptotal
ct$poverty_pct <- ct$poverty /ct$poptotal2

bkpoverty<-ct[,c(1,18,21)]
merged_data <-merge(bkpoverty,HOLC, by.x ="GEOID" , by.y = "geoid") 
merged_data_avg <-merged_data %>% group_by(holc_grade)%>% summarise(blkprg=mean(black_pct))
ggplot(merged_data_avg, aes(x=blkprg, fill=holc_grade )) + 
  geom_bar()

ggplot(merged_data, aes(x=holc_grade, y=holc_area, fill = holc_grade))+ geom_boxplot()