library(tidycensus)
## Warning: package 'tidycensus' 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(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
#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').
census_api_key("be13863f38ebdc792d7bf1fdbd7f8089b2a708f2", 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] "be13863f38ebdc792d7bf1fdbd7f8089b2a708f2"
var <- c(poptotal = 'B03002_001E',
hispanic = 'B03002_012E')
st <- "WA"
ct <- "King"
df <- get_acs(geography = "tract",variables = var,county = ct,state = st,output = "wide", year = 2022,geometry = TRUE)
## Getting data from the 2018-2022 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% | |=== | 4% | |=== | 5% | |==== | 5% | |===== | 7% | |===== | 8% | |====== | 8% | |====== | 9% | |======= | 10% | |======== | 12% | |========= | 12% | |========= | 13% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============= | 18% | |============= | 19% | |============== | 20% | |=============== | 21% | |=============== | 22% | |================ | 23% | |================= | 24% | |================= | 25% | |=================== | 27% | |==================== | 28% | |===================== | 29% | |===================== | 31% | |======================= | 32% | |========================== | 38% | |============================= | 42% | |================================ | 46% | |================================= | 46% | |================================= | 47% | |=================================== | 50% | |===================================== | 53% | |======================================== | 58% | |=========================================== | 62% | |============================================== | 66% | |================================================= | 70% | |=================================================== | 73% | |==================================================== | 75% | |===================================================== | 75% | |======================================================== | 80% | |========================================================== | 82% | |=========================================================== | 85% | |================================================================ | 92% | |==================================================================== | 97% | |===================================================================== | 98% | |======================================================================| 100%
df$hispanic_pct <- (df$hispanic / df$poptotal)
df <- df[!st_is_empty(df$geometry), ]
tm_shape(df) +
tm_polygons(col = "hispanic_pct", palette = "YlOrRd", title = "Hispanic %") +
tm_compass(type = "8star", position = c("right","top"), size = 1) +
tm_scale_bar(position = c("left", "bottom"))+
tm_layout(legend.position = c("center", "bottom"), legend.outside = TRUE, legend.outside.position = "right")
#2. Find any point shapefile data within the county and make a map with census tract as the background (2').
bike_racks <- st_read("C:/Users/witht/Documents/2024MethodsI/ProjectPart2/Bike_Racks/Bicycle_Racks.shp")
## Reading layer `Bicycle_Racks' from data source
## `C:\Users\witht\Documents\2024MethodsI\ProjectPart2\Bike_Racks\Bicycle_Racks.shp'
## using driver `ESRI Shapefile'
## replacing null geometries with empty geometries
## Simple feature collection with 3988 features and 29 fields (with 16 geometries empty)
## Geometry type: POINT
## Dimension: XYM
## Bounding box: xmin: 1249223 ymin: 185196.9 xmax: 1290757 ymax: 270742.7
## m_range: mmin: 0 mmax: 8226.603
## Projected CRS: NAD83(HARN) / Washington North (ftUS)
king_ct <- tracts(state = "WA", county = "King",cb=T)
## Retrieving data for the year 2022
tm_shape(king_ct)+
tm_borders()+
tm_shape(bike_racks)+
tm_dots(col="red")+
tm_layout(frame = FALSE)
## Warning: The shape bike_racks contains empty units (after reprojection).
#3. Find any line shapefile data within the county and make a map with census tract as the background (2').
bike_lanes <- st_read("C:/Users/witht/Documents/2024MethodsI/ProjectPart2/Bike_lanes/Existing_Bike_Facilities.shp")
## Reading layer `Existing_Bike_Facilities' from data source
## `C:\Users\witht\Documents\2024MethodsI\ProjectPart2\Bike_lanes\Existing_Bike_Facilities.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 3530 features and 55 fields
## Geometry type: LINESTRING
## Dimension: XY
## Bounding box: xmin: 1246042 ymin: 185366.6 xmax: 1293475 ymax: 271454.4
## Projected CRS: NAD83(HARN) / Washington North (ftUS)
tm_shape(king_ct)+
tm_borders()+
tm_shape(bike_lanes)+
tm_lines(lwd = 2, col= "green")+
tm_layout(frame=FALSE)
#4. Find any polygon shapefile data within the county and make a map with census tract as the background (2').
neighborhood <- st_read("C:/Users/witht/Documents/2024MethodsI/ProjectPart2/Neighborhood/Neighborhood_Map_Atlas_Neighborhoods.shp")
## Reading layer `Neighborhood_Map_Atlas_Neighborhoods' from data source
## `C:\Users\witht\Documents\2024MethodsI\ProjectPart2\Neighborhood\Neighborhood_Map_Atlas_Neighborhoods.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 94 features and 6 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 1245524 ymin: 184055.5 xmax: 1293764 ymax: 271598.5
## Projected CRS: NAD83(HARN) / Washington North (ftUS)
tm_shape(king_ct)+
tm_borders()+
tm_shape(neighborhood)+
tm_polygons(col = "L_HOOD",
palette = "Set3",
title = "Neighborhood") +
tm_layout(frame = FALSE,legend.outside = TRUE)
#5. Have overlay analysis for any two shapefile data layers in your study area (2').
bike_racks <- st_zm(bike_racks)
bike_racks <- st_make_valid(bike_racks)
bike_lanes <- st_make_valid(bike_lanes)
bike_racks<- st_transform(bike_racks,crs= 4326)
bike_lanes<- st_transform(bike_lanes, crs=4326)
bike_lane_buffered <- st_buffer(bike_lanes, dist = 6)
clipped_bike <-st_intersection(bike_racks, bike_lane_buffered)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
#6. Get Digital Elevation Model (DEM) data of a selected area of the county
library(raster)
## Loading required package: sp
DEM_king <- raster("C:/Users/witht/Documents/2024MethodsI/ProjectPart2/seattle_topography.tif")
#7. Read the DEM data and get the min and max of elevations (2').
DEM_king <- raster("C:/Users/witht/Documents/2024MethodsI/ProjectPart2/seattle_topography.tif")
DEM_king <- setMinMax(DEM_king)
cellStats(DEM_king, max)
## [1] 1347.69
#8. Make a map that shows areas with elevation lower than 350m (1').
plot(DEM_king < 350, main = "Elevation criteria",
col = c("#ffffff", "#0000ff"))
#9. Make a map that shows the slope degree of topography map is less than 3 (1').
plot(terrain(DEM_king, opt = "slope", unit = "degrees") < 3,
main = "Slope criteria",
col = c("#ffffff", "#ff9900"))
#10. Make a map that shows the aspect criteria of topography map is less than between 22.5 and 157.5 (2').
plot(terrain(DEM_king, opt = "aspect", unit = "degrees") > 22.5 &
terrain(DEM_king, opt = "aspect", unit = "degrees") < 157.5,
main = "Aspect criteria")
#11. Have write-up to summarize your findings (2’).
#In King County, Hispanic residents are primarily concentrated in the southwestern portion between Seattle and Tacoma, where they make up as much as 60% of the population in some census tracts, while the northern and eastern areas of the county have Hispanic populations below 20%. Within Seattle, bike racks are predominantly located in the city’s central core; noticeably more racks can be seen in northern neighborhoods compared to the southern ones. Bike lanes, however, appear to be evenly distributed across the city. Seattle’s Neighborhood Atlas divides the city into 20 neighborhoods of similar size. As a coastal city, much of the Seattle region lies below 350 meters in elevation and features a notably hilly terrain.