library(tidycensus)
library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.3 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ─────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
library(ggplot2)
library(classInt)
library(patchwork)
library(dplyr)
library(tmap)
library(tmaptools)
library(ggsn)
## Loading required package: grid
v15_Profile <- load_variables(2017 , "acs5/profile", cache = TRUE) #demographic profile tables
#Search for variables by keywords in the label
v15_Profile[grep(x = v15_Profile$label, "POVERTY"), c("name", "label")]
## # A tibble: 38 x 2
## name label
## <chr> <chr>
## 1 DP03_0119 Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE P…
## 2 DP03_0119P Percent Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME …
## 3 DP03_0120 Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE P…
## 4 DP03_0120P Percent Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME …
## 5 DP03_0121 Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE P…
## 6 DP03_0121P Percent Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME …
## 7 DP03_0122 Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE P…
## 8 DP03_0122P Percent Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME …
## 9 DP03_0123 Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE P…
## 10 DP03_0123P Percent Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME …
## # … with 28 more rows
v15_Profile[grep(x = v15_Profile$label, "Built 2000 to 2009"), c("name", "label")]
## # A tibble: 2 x 2
## name label
## <chr> <chr>
## 1 DP04_0019 Estimate!!YEAR STRUCTURE BUILT!!Total housing units!!Built 2000 to…
## 2 DP04_0019P Percent Estimate!!YEAR STRUCTURE BUILT!!Total housing units!!Built…
sa_acs<-get_acs(geography = "tract",
state="TX",
county = c("Bexar"),
year = 2017,
variables=c( "DP03_0062E") ,
geometry = T, output = "wide")
## Getting data from the 2013-2017 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using the ACS Data Profile
## Warning: `funs()` is deprecated as of dplyr 0.8.0.
## Please use a list of either functions or lambdas:
##
## # Simple named list:
## list(mean = mean, median = median)
##
## # Auto named with `tibble::lst()`:
## tibble::lst(mean, median)
##
## # Using lambdas
## list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
##
|
| | 0%
|
|= | 1%
|
|= | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 10%
|
|======== | 11%
|
|======== | 12%
|
|========= | 13%
|
|========== | 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%
|
|======================= | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 50%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 60%
|
|========================================== | 61%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================= | 71%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 79%
|
|======================================================== | 80%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 83%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 90%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 96%
|
|==================================================================== | 97%
|
|==================================================================== | 98%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 100%
#create a county FIPS code - 5 digit, making county identifier
sa_acs$county<-substr(sa_acs$GEOID, 1, 5)
#rename variables and filter missing cases
sa_acs2<-sa_acs%>%
mutate(medianinc= DP03_0062E) %>%
na.omit()
st_crs(sa_acs2)
## Coordinate Reference System:
## User input: NAD83
## wkt:
## GEOGCRS["NAD83",
## DATUM["North American Datum 1983",
## ELLIPSOID["GRS 1980",6378137,298.257222101,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["latitude",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["longitude",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4269]]
tm_shape(sa_acs2)+
tm_polygons("medianinc", title="Median Household Income", palette="Blues", style="quantile", n=5 ,legend.hist=T)+
tm_format("World", title="San Antonio Median Household Income Estimates - Quantile Breaks", legend.outside=T)+
tm_scale_bar()+
tm_compass()
#Extract two tracts
twtr<-sa_acs2%>%
filter(GEOID %in% c(48029181820, 48029110600))
twtr
## Simple feature collection with 2 features and 6 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -98.63226 ymin: 29.42167 xmax: -98.50083 ymax: 29.59091
## geographic CRS: NAD83
## GEOID NAME DP03_0062E DP03_0062M
## 1 48029110600 Census Tract 1106, Bexar County, Texas 13467 2460
## 2 48029181820 Census Tract 1818.20, Bexar County, Texas 37225 6833
## geometry county medianinc
## 1 MULTIPOLYGON (((-98.51358 2... 48029 13467
## 2 MULTIPOLYGON (((-98.63226 2... 48029 37225
# get centroid coordinates for the tracts
tr_co<-st_centroid(twtr)
## Warning in st_centroid.sf(twtr): st_centroid assumes attributes are constant
## over geometries of x
## Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
## of_largest_polygon): st_centroid does not give correct centroids for longitude/
## latitude data
tr_co
## Simple feature collection with 2 features and 6 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -98.61502 ymin: 29.43017 xmax: -98.50749 ymax: 29.57909
## geographic CRS: NAD83
## GEOID NAME DP03_0062E DP03_0062M
## 1 48029110600 Census Tract 1106, Bexar County, Texas 13467 2460
## 2 48029181820 Census Tract 1818.20, Bexar County, Texas 37225 6833
## geometry county medianinc
## 1 POINT (-98.50749 29.43017) 48029 13467
## 2 POINT (-98.61502 29.57909) 48029 37225
st_distance(tr_co)
## Units: [m]
## [,1] [,2]
## [1,] 0.00 19522.95
## [2,] 19522.95 0.00
twtr1<-st_transform(twtr, crs = 2278)
# get centroid coordinates for the tracts
tr_co1<-st_centroid(twtr1)
## Warning in st_centroid.sf(twtr1): st_centroid assumes attributes are constant
## over geometries of x
st_distance(tr_co1)
## Units: [US_survey_foot]
## [,1] [,2]
## [1,] 0.00 64043.26
## [2,] 64043.26 0.00
#feet to meters
#64043.26/3.281 =19519.43
twtr2<-st_transform(twtr, crs = 3083)
# get centroid coordinates for the tracts
tr_co2<-st_centroid(twtr2)
## Warning in st_centroid.sf(twtr2): st_centroid assumes attributes are constant
## over geometries of x
st_distance(tr_co2)
## Units: [m]
## [,1] [,2]
## [1,] 0.00 19536.81
## [2,] 19536.81 0.00
#We need to reproject data from Geographic coordinate system to the appropriate projection coordinate system to measure distance. If we map the small area precision becomes important. For San Antonio we use NAD 1983 State Plane Texas South Central FIPS 4204 Feet coordinate system.