#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)
#Q9 Find any polygon shapefile data within the county and make a map with census tract as the background
########polygons
tm_shape(Travis_segregation) +tm_fill(col = "Hispanic")+ tm_layout(title = "Hispanic Pop")
Travis_tracts
## Simple feature collection with 290 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -98.17298 ymin: 30.02345 xmax: -97.36954 ymax: 30.62825
## Geodetic CRS:  NAD83
## First 10 features:
##     STATEFP COUNTYFP TRACTCE             AFFGEOID       GEOID  NAME
## 6        48      453  002442 1400000US48453002442 48453002442 24.42
## 8        48      453  030300 1400000US48453030300 48453030300   303
## 80       48      453  002222 1400000US48453002222 48453002222 22.22
## 100      48      453  033700 1400000US48453033700 48453033700   337
## 115      48      453  001103 1400000US48453001103 48453001103 11.03
## 119      48      453  002443 1400000US48453002443 48453002443 24.43
## 178      48      453  002452 1400000US48453002452 48453002452 24.52
## 212      48      453  001312 1400000US48453001312 48453001312 13.12
## 217      48      453  040400 1400000US48453040400 48453040400   404
## 218      48      453  002316 1400000US48453002316 48453002316 23.16
##               NAMELSAD STUSPS    NAMELSADCO STATE_NAME LSAD   ALAND AWATER
## 6   Census Tract 24.42     TX Travis County      Texas   CT 1237089      0
## 8     Census Tract 303     TX Travis County      Texas   CT 2315778      0
## 80  Census Tract 22.22     TX Travis County      Texas   CT 2608468      0
## 100   Census Tract 337     TX Travis County      Texas   CT 2443338      0
## 115 Census Tract 11.03     TX Travis County      Texas   CT  838017 172901
## 119 Census Tract 24.43     TX Travis County      Texas   CT 1755323      0
## 178 Census Tract 24.52     TX Travis County      Texas   CT 9010295  87846
## 212 Census Tract 13.12     TX Travis County      Texas   CT 1968315      0
## 217   Census Tract 404     TX Travis County      Texas   CT 2498099      0
## 218 Census Tract 23.16     TX Travis County      Texas   CT  700495      0
##                           geometry
## 6   MULTIPOLYGON (((-97.76278 3...
## 8   MULTIPOLYGON (((-97.80674 3...
## 80  MULTIPOLYGON (((-97.64375 3...
## 100 MULTIPOLYGON (((-97.80674 3...
## 115 MULTIPOLYGON (((-97.74501 3...
## 119 MULTIPOLYGON (((-97.77712 3...
## 178 MULTIPOLYGON (((-97.64669 3...
## 212 MULTIPOLYGON (((-97.76898 3...
## 217 MULTIPOLYGON (((-97.74342 3...
## 218 MULTIPOLYGON (((-97.73295 3...
#Q10 Have overlay analysis between the point shapefile and the county's census tract shapefile so as to find the census tract ID of each point
#use overlay analysis to find the county for each landmark
Travis_ct <-tracts(state = "TX", county = "Travis",cb=T)
## Retrieving data for the year 2021
Travis_ct<-st_transform(Travis_ct,crs = 4326)
landmark_Travis_ct <- st_intersection(clipped_landmark_Travis, Travis_ct)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
#Q11 Get Digital Elevation Model (DEM) data of a selected area of the county through video
#Done

#Q12 Read the DEM data and get the min and max of elevations
library(raster)
## Loading required package: sp
wd <- ("C:/Users/haomi/Desktop/Urban Planning Methods I URP-5363-901-Fall 2023/Project 2/")
setwd(wd)
DEM <- raster(paste0(wd, "USGS_13_n31w098_20211103.tif"))
DEM <- setMinMax(DEM)
DEM
## class      : RasterLayer 
## dimensions : 10812, 10812, 116899344  (nrow, ncol, ncell)
## resolution : 9.259259e-05, 9.259259e-05  (x, y)
## extent     : -98.00056, -96.99944, 29.99944, 31.00056  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=NAD83 +no_defs 
## source     : USGS_13_n31w098_20211103.tif 
## names      : USGS_13_n31w098_20211103 
## values     : 79.33839, 398.0261  (min, max)
#Q13 Make a map that shows areas with elevation lower than 350m
plot(DEM < 350, main = "Elevation criteria",
     col = c("#ffffff", "#0000ff"))

#Q14 Make a map that shows the slop degree of topography map is less than 3
plot(terrain(DEM, opt = "slope", unit = "degrees") < 3, 
     main = "Slope criteria",
     col = c("#ffffff", "#ff9900"))

#Q15 Make a map that shows the aspect criteria of topography map is less than between 22.5 and 157.5
plot(terrain(DEM, opt = "aspect", unit = "degrees") > 22.5 &
       terrain(DEM, opt = "aspect", unit = "degrees") < 157.5, 
     main = "Aspect criteria")

#Q16 Submit your code and result to Rpubs using R markdown