options(repos = c(CRAN = "https://cloud.r-project.org"))

# Install the required packages if you haven't already
install.packages("tidycensus")
## 
## The downloaded binary packages are in
##  /var/folders/4b/1p0sp0rs33xg19wq8_5j5lm40000gn/T//RtmpXdHZ5w/downloaded_packages
install.packages("writexl")
## 
## The downloaded binary packages are in
##  /var/folders/4b/1p0sp0rs33xg19wq8_5j5lm40000gn/T//RtmpXdHZ5w/downloaded_packages
install.packages("ggplot2")
## 
## The downloaded binary packages are in
##  /var/folders/4b/1p0sp0rs33xg19wq8_5j5lm40000gn/T//RtmpXdHZ5w/downloaded_packages
install.packages("dplyr")
## 
## The downloaded binary packages are in
##  /var/folders/4b/1p0sp0rs33xg19wq8_5j5lm40000gn/T//RtmpXdHZ5w/downloaded_packages
# Load the required libraries
library(tidycensus)
library(writexl)
library(ggplot2)
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
census_api_key("f4f4801feced14583355d7a62646514912610e1d", install = TRUE, overwrite = TRUE)
## Your original .Renviron will be backed up and stored in your R HOME directory if needed.
## Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY"). 
## To use now, restart R or run `readRenviron("~/.Renviron")`
## [1] "f4f4801feced14583355d7a62646514912610e1d"
readRenviron("~/.Renviron")

# Get median age of males for Texas census tracts from the 2010 Decennial Census
median_age_males_tx <- get_decennial(
  geography = "tract",
  variables = "P013002",
  state = "TX",
  year = 2010,
  survey = "sf1"
)
## Getting data from the 2010 decennial Census
## Using Census Summary File 1
# View the first few rows of the data
head(median_age_males_tx)
## # A tibble: 6 × 4
##   GEOID       NAME                                     variable value
##   <chr>       <chr>                                    <chr>    <dbl>
## 1 48141000101 Census Tract 1.01, El Paso County, Texas P013002   34.8
## 2 48141000111 Census Tract 1.11, El Paso County, Texas P013002   31.6
## 3 48141000205 Census Tract 2.05, El Paso County, Texas P013002   28.2
## 4 48141000208 Census Tract 2.08, El Paso County, Texas P013002   26.3
## 5 48141000301 Census Tract 3.01, El Paso County, Texas P013002   27.6
## 6 48141000302 Census Tract 3.02, El Paso County, Texas P013002   29
# Export the data to an Excel file
write_xlsx(median_age_males_tx, "median_age_males_tx.xlsx")

# Get median household income for Bexar County, TX, from the 2018 ACS
median_income_bexar <- get_acs(
  geography = "tract",
  variables = "B19013_001E",
  state = "TX",
  county = "Bexar",
  year = 2018
)
## Getting data from the 2014-2018 5-year ACS
# View the first few rows of the data
head(median_income_bexar)
## # A tibble: 6 × 5
##   GEOID       NAME                                   variable   estimate   moe
##   <chr>       <chr>                                  <chr>         <dbl> <dbl>
## 1 48029110100 Census Tract 1101, Bexar County, Texas B19013_001    44050 13806
## 2 48029110300 Census Tract 1103, Bexar County, Texas B19013_001    34375 14338
## 3 48029110500 Census Tract 1105, Bexar County, Texas B19013_001    11360  2396
## 4 48029110600 Census Tract 1106, Bexar County, Texas B19013_001    14547  4007
## 5 48029110700 Census Tract 1107, Bexar County, Texas B19013_001    14891  5218
## 6 48029110800 Census Tract 1108, Bexar County, Texas B19013_001    29345  4495
# Get Hispanic population for all counties in Texas from the 2018 ACS
hispanic_pop_tx <- get_acs(
  geography = "county",
  variables = "B03002_012E",
  state = "TX",
  year = 2018
)
## Getting data from the 2014-2018 5-year ACS
# View the first few rows of the data
head(hispanic_pop_tx)
## # A tibble: 6 × 5
##   GEOID NAME                    variable   estimate   moe
##   <chr> <chr>                   <chr>         <dbl> <dbl>
## 1 48001 Anderson County, Texas  B03002_012    10142    NA
## 2 48003 Andrews County, Texas   B03002_012     9979    NA
## 3 48005 Angelina County, Texas  B03002_012    19174    NA
## 4 48007 Aransas County, Texas   B03002_012     6756    NA
## 5 48009 Archer County, Texas    B03002_012      727    NA
## 6 48011 Armstrong County, Texas B03002_012      131    69
# Rename the 'estimate' column to 'HispanicPop'
hispanic_pop_tx <- hispanic_pop_tx %>%
  rename(HispanicPop = estimate)

# Display all column names
colnames(hispanic_pop_tx)
## [1] "GEOID"       "NAME"        "variable"    "HispanicPop" "moe"
# Create a boxplot to show the distribution of Hispanic population
ggplot(hispanic_pop_tx, aes(x = "", y = HispanicPop)) +
  geom_boxplot() +
  labs(title = "Distribution of Hispanic Population Across Texas Counties",
       y = "Hispanic Population")

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"

cbg <- get_acs(geography = "block group", variables = var, count=ct,
               state = st,output="wide", year = 2021, geometry = TRUE)
## Getting data from the 2017-2021 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
##   |                                                                              |                                                                      |   0%  |                                                                              |                                                                      |   1%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |=======                                                               |  11%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |==============                                                        |  21%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  22%  |                                                                              |================                                                      |  23%  |                                                                              |================                                                      |  24%  |                                                                              |=================                                                     |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |===================                                                   |  28%  |                                                                              |====================                                                  |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |=====================                                                 |  31%  |                                                                              |======================                                                |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |=======================                                               |  34%  |                                                                              |========================                                              |  34%  |                                                                              |========================                                              |  35%  |                                                                              |=========================                                             |  35%  |                                                                              |=========================                                             |  36%  |                                                                              |==========================                                            |  37%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  39%  |                                                                              |============================                                          |  40%  |                                                                              |============================                                          |  41%  |                                                                              |=============================                                         |  41%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |=================================                                     |  48%  |                                                                              |==================================                                    |  48%  |                                                                              |==================================                                    |  49%  |                                                                              |===================================                                   |  49%  |                                                                              |===================================                                   |  50%  |                                                                              |===================================                                   |  51%  |                                                                              |====================================                                  |  51%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  54%  |                                                                              |======================================                                |  55%  |                                                                              |=======================================                               |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  57%  |                                                                              |========================================                              |  58%  |                                                                              |=========================================                             |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |==========================================                            |  61%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  62%  |                                                                              |============================================                          |  63%  |                                                                              |============================================                          |  64%  |                                                                              |=============================================                         |  64%  |                                                                              |=============================================                         |  65%  |                                                                              |==============================================                        |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |================================================                      |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |==================================================                    |  72%  |                                                                              |===================================================                   |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |===================================================                   |  74%  |                                                                              |====================================================                  |  74%  |                                                                              |====================================================                  |  75%  |                                                                              |=====================================================                 |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  77%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  79%  |                                                                              |========================================================              |  80%  |                                                                              |========================================================              |  81%  |                                                                              |=========================================================             |  81%  |                                                                              |=========================================================             |  82%  |                                                                              |==========================================================            |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  84%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  85%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |=============================================================         |  88%  |                                                                              |==============================================================        |  88%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  89%  |                                                                              |===============================================================       |  90%  |                                                                              |===============================================================       |  91%  |                                                                              |================================================================      |  91%  |                                                                              |================================================================      |  92%  |                                                                              |=================================================================     |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  94%  |                                                                              |==================================================================    |  95%  |                                                                              |===================================================================   |  95%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  97%  |                                                                              |====================================================================  |  98%  |                                                                              |===================================================================== |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================|  99%  |                                                                              |======================================================================| 100%
cbg$black_pct <-cbg$black/cbg$poptotal
cbg$white_pct <- cbg$white/cbg$poptotal
cbg$hispanic_pct <- cbg$hispanic/cbg$poptotal
cbg$poverty_pct <- cbg$poverty /cbg$poptotal2

###define poverty neighborhood
cbg$Poor <- ifelse(cbg$poverty_pct > 0.3, "Poor", "Nonpoor")

###define minority neighborhood
cbg$Race <- "Other"  # Default value
cbg$Race[cbg$white_pct > 0.5] <- "White"
cbg$Race[cbg$black_pct > 0.5] <- "Black"
cbg$Race[cbg$hispanic_pct > 0.5] <- "Hispanic"

cbg$race_poverty <- paste0(cbg$Poor, cbg$Race)

ggplot(cbg, aes(x=race_poverty, fill=race_poverty )) + 
  geom_bar()