knitr::opts_chunk$set(echo = TRUE)
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.4.2
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(plotly)
## Warning: package 'plotly' was built under R version 4.4.2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.4.2
library(sf)
## Warning: package 'sf' was built under R version 4.4.2
## Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
library(leaflet)
library(tigris)
## Warning: package 'tigris' was built under R version 4.4.2
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(tmap)
## Warning: package 'tmap' was built under R version 4.4.2
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
census_api_key("06f9cb24529c462c279e7d52e18ec7798e6bfeec")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
variables<-c(income = "B19013_001E", # Median household income
white = "B03002_003E", # White population
black = "B03002_004E", # Black or African American population
hispanic = "B03002_012E", # Hispanic or Latino population
total_pop = "B01003_001E", # Total population
poverty = "B17001_002E" # People below the poverty level
)
travis_data <- get_acs(geography = "tract",
variables = variables,
state = "TX",
county = "Travis",
year = 2020,
output = "wide",
geometry = TRUE
)
## 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% | |=== | 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% | |=============== | 22% | |================ | 22% | |================ | 23% | |================ | 24% | |================= | 24% | |================= | 25% | |================== | 25% | |================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 29% | |===================== | 30% | |====================== | 31% | |====================== | 32% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 39% | |============================ | 40% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 46% | |================================= | 47% | |================================= | 48% | |================================== | 49% | |=================================== | 50% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |====================================== | 54% | |====================================== | 55% | |======================================= | 56% | |======================================== | 57% | |========================================= | 58% | |========================================= | 59% | |========================================== | 60% | |=========================================== | 61% | |============================================ | 62% | |============================================ | 63% | |============================================== | 66% | |================================================= | 70% | |================================================== | 71% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 76% | |======================================================== | 80% | |========================================================== | 82% | |========================================================== | 84% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 89% | |=============================================================== | 90% | |=============================================================== | 91% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |=================================================================== | 96% | |===================================================================== | 98% | |======================================================================| 100%
#1. Make a map to show the spatial distribution of percentage of Hispanic population of the county you analyzed, and include compass and scale bar (2').----
travis_data <- travis_data %>%
mutate(percentage_hispanic = (hispanic / total_pop) * 100)
hisp_map <- tm_shape(travis_data) +
tm_borders() +
tm_fill("percentage_hispanic",
palette = "YlOrRd",
title = "% Hispanic",
style = "cont") +
tm_layout(main.title = "Spatial Distribution of Hispanic Population\nin Travis County, TX",
main.title.size = 1.2,
legend.position = c("left", "bottom"),
frame = F) +
tm_compass(type = "arrow", position = c("RIGHT", "TOP"), size = 1) +
tm_scale_bar(position = c("RIGHT", "BOTTOM"), size = 0.5)
## Warning: The argument size of tm_scale_bar is deprecated. It has been renamed
## to text.size
print(hisp_map)
#2. Find any point shapefile data within the county and make a map with census tract as the background (2').----
park_facilities<-st_read("Travis_Park_Facilities/Park_Facilities.shp")
## Reading layer `Park_Facilities' from data source
## `C:\Users\vmq938\OneDrive - University of Texas at San Antonio\methods1\Travis_Park_Facilities\Park_Facilities.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 707 features and 8 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 2991024 ymin: 10034790 xmax: 3194189 ymax: 10144570
## Projected CRS: NAD83 / Texas Central (ftUS)
park_facilities<-st_transform(park_facilities, crs=4326)
park_facilities_map <- tm_shape(travis_data) +
tm_borders() +
tm_shape(park_facilities) +
tm_dots(size = 0.05, col = "blue", alpha = 0.5, legend.show = FALSE) +
tm_layout(main.title = "Park Facilities in Travis County, TX",
main.title.size = 1.2,
legend.position = c("left", "bottom"),
frame = F)
print(park_facilities_map)
#3. Find any line shapefile data within the county and make a map with census tract as the background (2').----
roads <- st_read("Travis_County_Roads/Travis_County_Roads.shp")
## Reading layer `Travis_County_Roads' from data source
## `C:\Users\vmq938\OneDrive - University of Texas at San Antonio\methods1\Travis_County_Roads\Travis_County_Roads.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 13999 features and 35 fields
## Geometry type: MULTILINESTRING
## Dimension: XY
## Bounding box: xmin: 2978705 ymin: 9982087 xmax: 3230572 ymax: 10198600
## Projected CRS: NAD83 / Texas Central (ftUS)
roads <- st_transform(roads, crs=4326)
rural_roads <- roads %>%
filter(FNC_CLSS %in% c("Rural Local", "Rural Collector", "Rural Arterial", "Rural Minor Arterial", "Rural Major Collector"))
rural_rd_map <- tm_shape(travis_data) +
tm_borders() +
tm_shape(rural_roads) +
tm_lines(col = "red", lwd = 1, legend.show = FALSE) +
tm_layout(main.title = "Rural Roads in Travis County, TX",
main.title.size = 1.2,
legend.position = NULL,
frame = F)
print(rural_rd_map)
#4. Find any polygon shapefile data within the county and make a map with census tract as the background (2').----
warbler<-st_read("Travis_GCW_Habitat/Golden-cheeked_Warbler_Habitat.shp")
## Reading layer `Golden-cheeked_Warbler_Habitat' from data source
## `C:\Users\vmq938\OneDrive - University of Texas at San Antonio\methods1\Travis_GCW_Habitat\Golden-cheeked_Warbler_Habitat.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 465 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 2977857 ymin: 10013720 xmax: 3126086 ymax: 10200090
## Projected CRS: NAD83 / Texas Central (ftUS)
warbler<-st_make_valid(warbler)
warbler<-st_transform(warbler, crs=4326)
warbler_map <- tm_shape(travis_data) +
tm_borders() +
tm_shape(warbler) +
tm_borders(lwd=.5) +
tm_fill("ZONEDESC",
palette = c("Confirmed Habitat" = "gold",
"Unconfirmed Habitat" = "pink",
"Not Known to be Habitat" = "tan"),
title = "Habitat Status",
legend.show = TRUE) + # Show the legend
tm_layout(main.title = "Golden-Cheeked Warbler Habitat in Travis County",
main.title.size = 1.2,
legend.position = c("left", "bottom"),
frame = F)
print(warbler_map)
#5. Have overlay analysis for any two shapefile data layers in your study area (2').----
confirmed_habitat <- warbler[warbler$ZONEDESC == "Confirmed Habitat", ]
fac_in_habitat <- st_intersection(park_facilities, confirmed_habitat)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
num_fac_in_habitat <- nrow(fac_in_habitat)
print(paste("Number of park facilities within confirmed warbler habitat:", num_fac_in_habitat))
## [1] "Number of park facilities within confirmed warbler habitat: 113"
#6. Get Digital Elevation Model (DEM) data of a selected area of the county. #7. Read the DEM data and get the min and max of elevations (2').----
library(raster)
## Loading required package: sp
##
## Attaching package: 'raster'
## The following object is masked from 'package:plotly':
##
## select
## The following object is masked from 'package:dplyr':
##
## select
dem<-raster("USGS_13_n31w098_20211103.tif")
dem<-projectRaster(dem, crs=4326)
dem_downsampled <- aggregate(dem, fact = 10)
dem <- setMinMax(dem)
cellStats(dem, min)
## [1] 79.33921
cellStats(dem, max)
## [1] 397.9549
#8. Make a map that shows areas with elevation lower than 350m (1').----
low_elevation <- dem < 350
travis_data<-st_transform(travis_data, crs=4326)
low_elevation_map <- tm_shape(low_elevation) +
tm_raster(palette = c("white", "green", "darkgreen"), legend.show = FALSE) +
tm_shape(travis_data) +
tm_borders(col = "black") +
tm_layout(main.title = "Areas with Elevation Below 350m ",
main.title.size = 1.2)
print(low_elevation_map)
## stars object downsampled to 1000 by 1000 cells. See tm_shape manual (argument raster.downsample)
#9. Make a map that shows the slope degree of topography map is less than 3 (1').----
slope <- terrain(dem, opt = "slope", unit = "degrees")
low_slope <- slope < 3
low_slope_map <- tm_shape(low_slope) +
tm_raster(palette = c("white", "lightgreen", "darkgreen"), legend.show = FALSE) +
tm_shape(travis_data) +
tm_borders(col = "black") +
tm_layout(main.title = "Areas with Slope < 3 Degrees",
main.title.size = 1.2)
print(low_slope_map)
## stars object downsampled to 1000 by 1000 cells. See tm_shape manual (argument raster.downsample)
#10. Make a map that shows the aspect criteria of topography map is less than between 22.5 and 157.5 (2').----
aspect <- terrain(dem, opt = "aspect", unit = "degrees")
low_aspect <- aspect > 22.5 & aspect < 157.5
aspect_map <- tm_shape(low_aspect) +
tm_raster(palette = c("white", "lightgreen", "darkgreen"), legend.show = FALSE) +
tm_shape(travis_data) +
tm_borders(col = "black") +
tm_layout(main.title = "Areas with Aspect between 22.5° and 157.5°",
main.title.size = 1.2)
print(aspect_map)
## stars object downsampled to 1000 by 1000 cells. See tm_shape manual (argument raster.downsample)
#11. Have write-up to summarize your findings (2').----
#This script provides a comprehensive spatial analysis of Travis County, TX, integrating demographic, environmental, and topographic data. The analysis includes visualizations of the percentage of Hispanic population across census tracts, the location of park facilities, rural roads, and Golden-Cheeked Warbler habitat areas. Overlay analysis revealed the number of park facilities within confirmed warbler habitats. Topographic analyses, including a Digital Elevation Model (DEM), identified areas with elevations below 350m, slopes less than 3 degrees, and aspect between 22.5° and 157.5°, all crucial for understanding land use suitability, flood risk, and conservation priorities. These findings were visualized using tmap and sf/raster packages, combining census data with natural features to explore spatial relationships in the county.