#Project 2
library(tidycensus)
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(tmap)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
## (status 2 uses the sf package in place of rgdal)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
#median household income= B19013_001E
#Hispanic population= B03002_012E
#Non-Hispanic African American population= B03002_004E
#male=B01001_002E
#female= B01001_026E
#total population=B01003_001E
#median age= B01002_001E Estimate!!Median age --!!Total: MEDIAN AGE BY SEX
var=c('B19013_001E','B03002_012E','B03002_004E','B01001_002E','B01001_026E','B01003_001E','B01002_001E')
Travis_segregation <- get_acs(geography = "tract", variables = var, county = "Travis",
state = "TX",output="wide", 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%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|==== | 5%
|
|==== | 6%
|
|===== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========== | 14%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 20%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================= | 24%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 30%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 40%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 43%
|
|============================== | 44%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 46%
|
|================================= | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 70%
|
|================================================= | 71%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|=============================================================== | 91%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
names(Travis_segregation)[3] <- 'MHHincome'
names(Travis_segregation)[5] <- 'Hispanic'
names(Travis_segregation)[7] <- 'Black or African American'
names(Travis_segregation)[9] <- 'Male'
names(Travis_segregation)[11] <- 'Female'
names(Travis_segregation)[13] <- 'TotalPop'
names(Travis_segregation)[15] <- 'MAge'
Travis_segregation$B19013_001M<- NULL
Travis_segregation$B03002_001M<- NULL
Travis_segregation$B03002_004M<- NULL
Travis_segregation$B01001_002M<- NULL
Travis_segregation$B01001_026M<- NULL
Travis_segregation$B01003_001M<- NULL
Travis_segregation$B01002_001M<- NULL
#Q1 Redo the analysis for mapping Hispanic population and add histogram in the map
tm_shape(Travis_segregation) +tm_fill(col = "Hispanic")+ tm_layout(title = "Hispanic Pop")
#Add histogram
tm_shape(Travis_segregation) + tm_fill(col = "Hispanic", legend.hist = TRUE)+ tm_layout(title = "Hispanic Pop") +
tm_fill(col = "Hispanic",legend.hist = TRUE)+
tm_layout(title = "Hispanic Pop") +
tm_layout(legend.outside = TRUE,frame = FALSE)
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
#Q2 Following Q1, add border for census tracts
tm_shape(Travis_segregation) +
tm_fill(col = "Hispanic",legend.hist = TRUE)+
tm_layout(title = "Hispanic Pop")+
#add border
tm_borders(alpha=.4) + tm_layout(legend.outside = TRUE,frame = FALSE)
#Q3 Following Q2, add compass and scale bar for the map
tm_shape(Travis_segregation) +
tm_fill(col = "Hispanic",legend.hist = TRUE)+
tm_layout(title = "Hispanic Pop")+
tm_borders(alpha=.4) +
#add compass
tm_compass(type = "8star",
position = c("RIGHT", "BOTTOM"),
show.labels = 2,
text.size = 0.6) +
tm_layout(legend.outside = TRUE,frame = FALSE)+
#add scale bars
tm_scale_bar(position = c("RIGHT", "BOTTOM"))
#Q4 Following Q3, set color intervals as "red" for the map
tm_shape(Travis_segregation) +
tm_fill(col = "Hispanic",legend.hist = TRUE)+
tm_layout(title = "Hispanic Pop")+
#set color intervals as "red"
tm_borders(alpha=.4,lwd = 2, col = "red") +
tm_compass(type = "8star",
position = c("RIGHT", "BOTTOM"),
show.labels = 2,
text.size = 0.6) +
tm_layout(legend.outside = TRUE,frame = FALSE)+
tm_scale_bar(position = c("RIGHT", "BOTTOM"))
#Q5 Make an interactive map to show the spatial distribution of percentage of Hispanic population
#Make a map to show the spatial distribution of percentage of Hispanic population
Travis_segregation$pct_Hispanic <- 100*Travis_segregation$Hispanic/Travis_segregation$TotalPop
tm_shape(Travis_segregation) +tm_fill(col = "pct_Hispanic")+ tm_layout(title = "Hispanic Percent")
#Make an interactive map
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(Travis_segregation) +tm_fill(col = "pct_Hispanic")+ tm_layout(title = "Hispanic Percent")
#Q6 Retrieve the census tract shapefile data of the county
Travis_tracts<- tracts(state = "TX", county = "Travis",cb=T)
## Retrieving data for the year 2021
#Q7 Find any point shapefile data within the county and make a map with census tract as the background
#get the boundary of Travis county
texas_counties<-counties(state = "Texas",cb=T)
## Retrieving data for the year 2021
##
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================ | 24%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 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%
|
|============================================= | 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%
|
|============================================================= | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|=============================================================== | 91%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|==================================================================== | 98%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
Travis<-texas_counties[texas_counties$NAME=="Travis",]
Travis<-st_transform(Travis,crs = 4326) #define the coordinate reference system
#get texas landmarks
tx_landmarks <- landmarks(state = "TX",type = 'point')
## Retrieving data for the year 2021
##
|
| | 0%
|
|= | 2%
|
|== | 3%
|
|==== | 5%
|
|===== | 7%
|
|====== | 9%
|
|======= | 10%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|=========== | 16%
|
|============ | 17%
|
|============= | 19%
|
|============== | 20%
|
|================ | 22%
|
|================= | 24%
|
|================== | 26%
|
|=================== | 27%
|
|==================== | 29%
|
|===================== | 30%
|
|====================== | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|========================= | 36%
|
|========================== | 37%
|
|=========================== | 39%
|
|============================ | 41%
|
|============================= | 41%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|=================================== | 49%
|
|=================================== | 50%
|
|=================================== | 51%
|
|===================================== | 53%
|
|====================================== | 54%
|
|======================================= | 56%
|
|======================================== | 57%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================== | 71%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|===================================================== | 75%
|
|====================================================== | 76%
|
|====================================================== | 78%
|
|======================================================== | 79%
|
|======================================================== | 80%
|
|========================================================= | 81%
|
|========================================================== | 83%
|
|=========================================================== | 84%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 90%
|
|================================================================ | 91%
|
|================================================================= | 93%
|
|================================================================== | 95%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|======================================================================| 100%
tx_landmarks<-st_transform(tx_landmarks,crs = 4326) #define the coordinate reference system
texas_counties <- counties(state = "TX",cb=T)
## Retrieving data for the year 2021
texas_counties<-st_transform(texas_counties,crs = 4326)
# use clip analysis to have the tx_landmarks in Travis county
clipped_landmark_Travis <- st_intersection(tx_landmarks, Travis)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
#plot the landmark for Travis
tm_shape(Travis) +
tm_borders() + # Add borders for polygons (customize as needed)
tm_shape(Travis_tracts) +
tm_dots(size = 0.02, col = "red", alpha = 0.7) + # Customize point appearance
tm_basemap() +
tm_layout(title = "Travis Landmarks", frame = FALSE)
#Q8 Find any line shapefile data within the county and make a map with census tract as the background
#####lines
bike_paths<-st_read(dsn = "C:/Users/haomi/Desktop/Urban Planning Methods I URP-5363-901-Fall 2023/Project 2/Park_Trails_and_Roads/Park_Trails_and_Roads.shp")
## Reading layer `Park_Trails_and_Roads' from data source
## `C:\Users\haomi\Desktop\Urban Planning Methods I URP-5363-901-Fall 2023\Project 2\Park_Trails_and_Roads\Park_Trails_and_Roads.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 364 features and 14 fields
## Geometry type: MULTILINESTRING
## Dimension: XY
## Bounding box: xmin: 2989166 ymin: 10035310 xmax: 3194239 ymax: 10143610
## Projected CRS: NAD83 / Texas Central (ftUS)
tmap_options(check.and.fix = TRUE)
buffer_distance <- 0.01 # adjust as needed
lines_polygon <- st_buffer(bike_paths, dist = buffer_distance)
tm_shape(lines_polygon) +
tm_borders() +
tm_layout(frame = FALSE)
tm_shape(Travis) +
tm_borders(alpha=.4,lwd = 2, col = "red") +
tm_shape(lines_polygon) +
tm_borders(lwd = 2, col = "red") + # Customize line appearance
tm_layout(frame = FALSE)