# 1. Download the "2017 BOND PROGRAM LINE " shapefile data from https://www.sanantonio.gov/GIS/GISDataLinks to an external site and visualize the lines (in green) with bexar county census tracts as the background.
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(tidycensus)
library(sf)
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
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
texas_counties <- counties(state = "TX", cb = TRUE)
## Retrieving data for the year 2022
## | | | 0% | | | 1% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 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% | |=================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 29% | |===================== | 30% | |===================== | 31% | |====================== | 31% | |====================== | 32% | |======================= | 32% | |======================= | 33% | |======================= | 34% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 39% | |============================ | 40% | |============================ | 41% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |============================== | 44% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 46% | |================================= | 47% | |================================= | 48% | |================================== | 48% | |================================== | 49% | |=================================== | 49% | |=================================== | 50% | |=================================== | 51% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |===================================== | 54% | |====================================== | 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% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |========================================================== | 84% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 89% | |=============================================================== | 90% | |=============================================================== | 91% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 99% | |======================================================================| 100%
bexar <- texas_counties[texas_counties$NAME == "Bexar",]
bexar <- st_transform(bexar, crs = 4326)
bond_lines <- st_read("C:/Users/cruzs/Downloads/BondProjectLines2017/BondProjectLines2017")
## Reading layer `BondProjectLines2017' from data source
## `C:\Users\cruzs\Downloads\BondProjectLines2017\BondProjectLines2017'
## using driver `ESRI Shapefile'
## Simple feature collection with 69 features and 24 fields
## Geometry type: MULTILINESTRING
## Dimension: XY
## Bounding box: xmin: 2078269 ymin: 13665110 xmax: 2168346 ymax: 13783530
## Projected CRS: NAD83 / Texas South Central (ftUS)
bond_lines <- st_transform(bond_lines, crs = 4326)
tm_shape(bexar) +
tm_borders(col = "black", lwd = 2) +
tm_shape(bond_lines) +
tm_lines(col = "green") +
tm_layout(title = "2017 Bond Program Lines in Bexar County")
# 2. Download the "LIBRARIES " shapefile data from https://www.sanantonio.gov/GIS/GISDataLinks to an external site. to an external site. and visualize the library point (in red) with Bexar county census tracts as the background. Make the map interactive using tmap_mode("view")
libraries <- st_read("C:/Users/cruzs/Downloads/Libraries/Libraries")
## Reading layer `Libraries' from data source
## `C:\Users\cruzs\Downloads\Libraries\Libraries' using driver `ESRI Shapefile'
## Simple feature collection with 30 features and 9 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 2075828 ymin: 13669570 xmax: 2170361 ymax: 13779280
## Projected CRS: NAD83 / Texas South Central (ftUS)
libraries <- st_transform(libraries, crs = 4326)
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(bexar) +
tm_borders(col = "black", lwd = 2) +
tm_shape(libraries) +
tm_dots(size = 0.05, col = "red", alpha = 0.7) +
tm_layout(title = "Libraries in Bexar County")
# 3. Retrieve the census block group data for Travis County, TX using get_acs(), including variables like total population and poverty population. Calculate the ratio of poverty level to total population (poverty/total), and visualize this variable using polygons on a map, applying a custom color palette with a gradient of green to red (e.g., palette = "RdYlGn")
library(tidycensus)
library(dplyr)
library(tidyr)
library(tmap)
variables <- c(total_pop = "B01003_001", poverty_pop = "B17001_002")
travis_data <- get_acs(
geography = "block group",
variables = variables,
state = "TX",
county = "Travis",
geometry = TRUE,
year = 2020
)
## Getting data from the 2016-2020 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% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 16% | |============ | 17% | |============ | 18% | |============= | 18% | |============= | 19% | |============== | 20% | |============== | 21% | |=============== | 21% | |=============== | 22% | |================ | 22% | |================ | 23% | |================= | 24% | |================= | 25% | |================== | 25% | |================== | 26% | |=================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 29% | |===================== | 30% | |===================== | 31% | |====================== | 31% | |====================== | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 39% | |============================ | 40% | |============================ | 41% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |============================== | 44% | |=============================== | 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% | |=============================================== | 66% | |=============================================== | 67% | |=============================================== | 68% | |================================================ | 68% | |================================================ | 69% | |================================================= | 69% | |================================================= | 70% | |================================================= | 71% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 89% | |=============================================================== | 90% | |=============================================================== | 91% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================= | 94% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 99% | |======================================================================| 100%
travis_data <- travis_data %>%
pivot_wider(names_from = variable, values_from = estimate) %>%
mutate(poverty_ratio = poverty_pop / total_pop) %>%
mutate(poverty_ratio = ifelse(is.na(poverty_ratio), 0, poverty_ratio))
head(travis_data)
## Simple feature collection with 6 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -97.74223 ymin: 30.13383 xmax: -97.63452 ymax: 30.30053
## Geodetic CRS: NAD83
## # A tibble: 6 × 7
## GEOID NAME moe geometry total_pop poverty_pop
## <chr> <chr> <dbl> <MULTIPOLYGON [°]> <dbl> <dbl>
## 1 484530024492 Block Grou… 1620 (((-97.69299 30.16425, -… 3443 NA
## 2 484530024492 Block Grou… NA (((-97.69299 30.16425, -… NA NA
## 3 484530005002 Block Grou… 432 (((-97.73508 30.29814, -… 1168 NA
## 4 484530005002 Block Grou… NA (((-97.73508 30.29814, -… NA NA
## 5 484530024132 Block Grou… 582 (((-97.74223 30.20387, -… 1846 NA
## 6 484530024132 Block Grou… NA (((-97.74223 30.20387, -… NA NA
## # ℹ 1 more variable: poverty_ratio <dbl>
tm_shape(travis_data) +
tm_polygons("poverty_ratio",
palette = "RdYlGn",
title = "Poverty Ratio",
style = "cont",
contrast = c(0.2, 1)) +
tm_layout(title = "Poverty Ratio in Travis County, TX",)
# 4. Make the coordinate systems of Bexar Couny census tract and LIBRARY the same, then use overlay analysis to find the census tract for each library.
bexar_ct <- tracts(state = "TX", county = "Bexar", cb = TRUE)
## Retrieving data for the year 2022
## | | | 0% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 7% | |===== | 8% | |====== | 8% | |====== | 9% | |======= | 9% | |======= | 10% | |======= | 11% | |======== | 11% | |======== | 12% | |========= | 12% | |========= | 13% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 17% | |============= | 18% | |============= | 19% | |============== | 19% | |============== | 20% | |=============== | 21% | |=============== | 22% | |================ | 22% | |================ | 23% | |================= | 25% | |================== | 25% | |================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 30% | |===================== | 31% | |====================== | 31% | |====================== | 32% | |======================= | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 39% | |============================ | 40% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 47% | |================================= | 48% | |================================== | 48% | |================================== | 49% | |=================================== | 50% | |=================================== | 51% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |===================================== | 54% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 56% | |======================================== | 57% | |======================================== | 58% | |========================================= | 58% | |========================================= | 59% | |========================================== | 59% | |========================================== | 60% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 63% | |============================================= | 64% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 67% | |================================================ | 68% | |================================================ | 69% | |================================================= | 70% | |================================================= | 71% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 79% | |======================================================== | 80% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 92% | |================================================================= | 93% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 99% | |======================================================================| 100%
bexar_ct <- st_transform(bexar_ct, crs = 4326)
libraries <- st_transform(libraries, crs = st_crs(bexar_ct))
library_analysis <- st_intersection(libraries, bexar_ct)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
tract_library_counts <- library_analysis %>%
group_by(GEOID) %>%
summarise(library_count = n())
print(tract_library_counts)
## Simple feature collection with 29 features and 2 fields
## Geometry type: GEOMETRY
## Dimension: XY
## Bounding box: xmin: -98.66264 ymin: 29.33487 xmax: -98.36605 ymax: 29.63648
## Geodetic CRS: WGS 84
## # A tibble: 29 × 3
## GEOID library_count geometry
## <chr> <int> <GEOMETRY [°]>
## 1 48029110100 2 MULTIPOINT ((-98.48894 29.42293), (-98.49251 29.43…
## 2 48029120902 1 POINT (-98.43386 29.51066)
## 3 48029121123 1 POINT (-98.47332 29.56965)
## 4 48029121205 1 POINT (-98.4015 29.54507)
## 5 48029121403 1 POINT (-98.38969 29.5094)
## 6 48029121812 1 POINT (-98.3725 29.57904)
## 7 48029121905 1 POINT (-98.44901 29.63615)
## 8 48029130800 1 POINT (-98.43455 29.42002)
## 9 48029131401 1 POINT (-98.36605 29.39328)
## 10 48029140900 1 POINT (-98.45797 29.38146)
## # ℹ 19 more rows