library(tidycensus)
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
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')
library(ggplot2)
library(data.table)
library(tmap)
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
## 
##     between, first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#Q1

census_api_key("0d539976d5203a96fa55bbf4421110d4b3db3648",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] "0d539976d5203a96fa55bbf4421110d4b3db3648"
var <- c('B19013_001E','B03002_012E','B03002_004E','B01001_002E','B01001_026E','B01003_001E','B01002_001E')
Travis_County <- 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%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  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%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
names(Travis_County)[3] <- 'MedIncome'
names(Travis_County)[5] <- 'Hispanic'
names(Travis_County)[7] <- 'Black'
names(Travis_County)[9] <- 'Male'
names(Travis_County)[11] <- 'Female'
names(Travis_County)[13] <- 'TotalPop'
names(Travis_County)[15] <- 'MedAge'

Travis_County$B19013_001M <- NULL
Travis_County$B03002_012M <- NULL
Travis_County$B03002_004M <- NULL
Travis_County$B01001_002M <- NULL
Travis_County$B01001_026M <- NULL
Travis_County$B01003_001M <- NULL
Travis_County$B01002_001M <- NULL

Travis_County$Percent_Hispanic <- 100*Travis_County$Hispanic/Travis_County$TotalPop

tm_shape(Travis_County) +
  tm_fill(col = "Percent_Hispanic",legend.hist=TRUE)+ 
  tm_layout(title = "Percent Hispanic",legend.outside=TRUE, frame=FALSE)

#Q2

tm_shape(Travis_County) + 
  tm_fill(col = "Percent_Hispanic",legend.hist=TRUE) + 
  tm_layout(title = "Percent Hispanic",legend.outside=TRUE, frame=FALSE) + 
  tm_borders(alpha = .5) + 
  tm_layout(frame=FALSE)

#Q3

tm_shape(Travis_County) + 
  tm_fill(col = "Percent_Hispanic",legend.hist=TRUE) + 
  tm_layout(title = "Percent Hispanic",legend.outside=TRUE, frame=FALSE) + 
  tm_borders(alpha = .5) + 
  tm_layout(frame=FALSE) +
  tm_compass(type = "arrow", 
             position = c("RIGHT", "BOTTOM"), 
             show.labels = 1,
             text.size = .5) +
  tm_scale_bar(position = c("LEFT","BOTTOM"),
              text.size = .4)

#Q4

tm_shape(Travis_County) + 
  tm_fill(col = "Percent_Hispanic",palette = "Reds", legend.hist=TRUE) + 
  tm_layout(title = "Percent Hispanic",legend.outside=TRUE, frame=FALSE) + 
  tm_borders(alpha = .5) + 
  tm_layout(frame=FALSE) +
  tm_compass(type = "arrow", 
             position = c("RIGHT", "BOTTOM"), 
             show.labels = 1,
             text.size = .5) +
  tm_scale_bar(position = c("LEFT","BOTTOM"),
               text.size = .4)

#Q5

tmap_mode("view") + tm_shape(Travis_County) + 
  tm_fill(col = "Percent_Hispanic",palette = "Reds", legend.hist=TRUE) + 
  tm_layout(title = "Percent Hispanic",legend.outside=TRUE, frame=FALSE) + 
  tm_borders(alpha = .5) + 
  tm_layout(frame=FALSE)
## tmap mode set to interactive viewing
#Q6

travis_tracts <- tracts(state = "TX", county = "Travis",cb=T)
## Retrieving data for the year 2021
#Q7

Park_Facilities <-st_read(dsn='C:/Users/18608/Downloads/Park_Facilities/Park_Facilities.shp')
## Reading layer `Park_Facilities' from data source 
##   `C:\Users\18608\Downloads\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)
tm_shape(travis_tracts)+
  tm_borders()+
  tm_shape(Park_Facilities)+
  tm_dots(size = .03,shape = 19, col = "red", alpha= 1)+
  tm_layout(title = "Park Facilities", frame = FALSE)
#Q8

Park_Trails_and_Roads <-st_read(dsn='C:/Users/18608/Downloads/Park_Trails_and_Roads/Park_Trails_and_Roads.shp')
## Reading layer `Park_Trails_and_Roads' from data source 
##   `C:\Users\18608\Downloads\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)
tm_shape(travis_tracts)+
  tm_borders()+
  tm_shape(Park_Trails_and_Roads)+
  tm_lines(size = .03, col = "red", alpha= 1)+
  tm_layout(title = "Park Trails and Roads", frame = FALSE)
#Q9

Municipal_Jurisdictions_Boundaries <-st_read(dsn='C:/Users/18608/Downloads/Municipal_Jurisdictions_Boundaries/Municipal_Jurisdictions_Boundaries.shp')
## Reading layer `Municipal_Jurisdictions_Boundaries' from data source 
##   `C:\Users\18608\Downloads\Municipal_Jurisdictions_Boundaries\Municipal_Jurisdictions_Boundaries.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 22 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 3013525 ymin: 9984338 xmax: 3223609 ymax: 10193220
## Projected CRS: NAD83 / Texas Central (ftUS)
tm_shape(travis_tracts)+
  tmap_options(check.and.fix = TRUE)+
  tm_borders()+
  tm_shape(Municipal_Jurisdictions_Boundaries)+
  tm_polygons(col = "red", alpha= .5)+
  tm_layout(title = "Municipal Jurisdictions Boundaries", frame = FALSE)
## Warning: The shape Municipal_Jurisdictions_Boundaries is invalid (after
## reprojection). See sf::st_is_valid
#Q10

travis_tracts <- st_transform(travis_tracts, crs = 4326)
Park_Facilities <- st_transform(Park_Facilities, crs = 4326)
clipped_Park_Facilities <- st_intersection(Park_Facilities,travis_tracts)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
tm_shape(travis_tracts)+
  tm_borders()+
  tm_shape(clipped_Park_Facilities)+
  tm_dots(size = .03,shape = 19, col = "red", alpha= 1)+
  tm_layout(title = "Park Facilities")
plot(clipped_Park_Facilities["NAMELSAD"])

#Q11

library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
DEM_Austin <-raster('C:/Users/18608/Downloads/USGS_13_n31w098_20211103.tif')
plot(DEM_Austin)

#Q12

DEM_Austin <-setMinMax(DEM_Austin)
cellStats(DEM_Austin, min)
## [1] 79.33839
cellStats(DEM_Austin, max)
## [1] 398.0261
#Q13

plot(DEM_Austin < 350, main= "Elevation Criteria")

#Q14

plot(terrain(DEM_Austin, opt= "slope", unit= "degrees") <3, main= "Slope Criteria")

#Q15

plot(terrain(DEM_Austin, opt= "slope", unit= "degrees") >22.5 &
     terrain(DEM_Austin, opt= "slope", unit= "degrees") <157.5, main="Aspect Criteria")