# 1. Redo the analysis for mapping Hispanic population and add histogram in the map (2').

library(tidycensus)
census_api_key("bd2af251ff3d1ee389e731f6d917f605cb300fdb", overwrite = "TRUE") 
## To install your API key for use in future sessions, run this function with `install = TRUE`.
var=c('B03002_012E','B03002_004E','B05003_002E', 'B05003_013E','B01001_001E', 'B01002I_001E', 'B19013I_001E') 

Miami_segregation <- get_acs(geography = "tract", variables = var, county = "Miami-Dade",
                             state = "FL",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%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
names(Miami_segregation)[3] <- 'Hispanic'
names(Miami_segregation)[5] <- 'Black or African American'
names(Miami_segregation)[7] <- 'Male'
names(Miami_segregation)[9] <- 'Female'
names(Miami_segregation)[11] <- 'TotalPop'
names(Miami_segregation)[13] <- 'MedianAge'
names(Miami_segregation)[15] <- 'MedianHouseholdIncome'

Miami_segregation$B03002_012M <- NULL
Miami_segregation$B03002_004M <- NULL
Miami_segregation$B05003_002M <- NULL
Miami_segregation$B05003_013M <- NULL
Miami_segregation$B01001_001M <- NULL
Miami_segregation$B01002I_001M <- NULL
Miami_segregation$B19013I_001M <- NULL

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(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
Miami_segregation$pct_Hisp <- 100*Miami_segregation$Hispanic/Miami_segregation$TotalPop
Miami_segregation <- Miami_segregation[-707,]
tm_shape(Miami_segregation) +tm_fill(col = "pct_Hisp")+ tm_layout(title = "Hispanic Percent")

tm_shape(Miami_segregation) +
  tm_fill(col = "Hispanic",legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+
  tm_layout(legend.outside = TRUE,frame = FALSE)

# 2. Following Q1, add border for census tracts  (1').

tm_shape(Miami_segregation) +
  tm_fill(col = "Hispanic",legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_borders(alpha=.4) +
  tm_layout(legend.outside = TRUE,frame = FALSE)

# 3. Following Q2, add compass and scale bar for the map  (1').

tmap_mode("plot")
## tmap mode set to plotting
tm_shape(Miami_segregation) +
  tm_fill(col = "Hispanic", legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_borders(alpha=.4) +
  tm_compass(type = "8star", 
             position = c("RIGHT", "BOTTOM"),
             show.labels = 2,
             text.size = 0.6) +
  tm_layout(legend.outside = TRUE,frame = TRUE)+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))

# 4. Following Q3, set color intervals as "red" for the map (1').

tm_shape(Miami_segregation) +
  tm_fill(col = "Hispanic", palette = "Reds", legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_borders(alpha=.4) +
  tm_compass(type = "8star", 
             position = c("RIGHT", "BOTTOM"),
             show.labels = 2,
             text.size = 0.6) +
  tm_layout(legend.outside = TRUE,frame = TRUE)+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))

# 5. Make an interactive map to show the spatial distribution of percentage of Hispanic population (2').

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(Miami_segregation) +
  tm_fill(col = "Hispanic", palette = "Reds", legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_borders(alpha=.4) +
  tm_layout(legend.outside = TRUE,frame = TRUE)+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))
# 6. Retrieve the census tract shapefile data of the county (1').

census_tract <-st_read(dsn = "/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Project 2/Census Tract Data/Tract.shp")
## Reading layer `Tract' from data source 
##   `/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Project 2/Census Tract Data/Tract.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 707 features and 74 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -80.8736 ymin: 25.13742 xmax: -80.06279 ymax: 25.97944
## Geodetic CRS:  WGS 84
# 7. Find any point shapefile data within the county and make a map with census tract as the background (2').

library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
MIA_Library <-st_read(dsn = "/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Project 2/County_Library/Library.shp")
## Reading layer `Library' from data source 
##   `/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Project 2/County_Library/Library.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 50 features and 25 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -80.44164 ymin: 25.50632 xmax: -80.12072 ymax: 25.9604
## Geodetic CRS:  WGS 84
MIA_Library <- st_transform(MIA_Library, crs=4326)

Miami_tracts<- tracts(state = "FL", county = "Miami-Dade",cb=T)
## Retrieving data for the year 2021
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(Miami_tracts) +
  tm_borders() +  # Add borders for polygons (customize as needed)
  tm_shape(MIA_Library) +
  tm_dots(size = 0.02, col = "red", alpha = 0.7) +  # Customize point appearance
  tm_layout(title = "Miami-Dade Libraries", frame = FALSE)
# 8. Find any line shapefile data within the county and make a map with census tract as the background (2').

bike_lanes <-st_read(dsn = "/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Project 2/Bike_lane/BikeLane.shp")
## Reading layer `BikeLane' from data source 
##   `/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Project 2/Bike_Lane/BikeLane.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 178 features and 5 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: -80.48534 ymin: 25.43657 xmax: -80.12091 ymax: 25.97454
## Geodetic CRS:  WGS 84
bike_lanes <- st_transform(bike_lanes, crs=4326)

Miami_tracts<- tracts(state = "FL", county = "Miami-Dade",cb=T)
## Retrieving data for the year 2021
buffer_distance <- 0.5 # adjust as needed
lines_polygon <- st_buffer(bike_lanes, dist = buffer_distance)

tm_shape(Miami_tracts) +
  tm_borders() +
  tm_shape(lines_polygon) +
  tm_borders(lwd = 2, col = "green") +  # Customize line appearance
  tm_layout(frame = FALSE)
# 9. Find any polygon shapefile data within the county and make a map with census tract as the background (2').
Urban_Center <-st_read(dsn = "/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Project 2/Urban_Center_Polygon/Urban_Center_Polygon.shp")
## Reading layer `Urban_Center_Polygon' from data source 
##   `/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Project 2/Urban_Center_Polygon/Urban_Center_Polygon.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 2915 features and 18 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -80.45454 ymin: 25.49208 xmax: -80.14713 ymax: 25.9688
## Geodetic CRS:  WGS 84
Urban_Center <- st_transform(Urban_Center, crs=4326)

Miami_tracts<- tracts(state = "FL", county = "Miami-Dade",cb=T)
## Retrieving data for the year 2021
tm_shape(Miami_tracts) +
  tm_borders() +
  tm_shape(Urban_Center) +
  tm_borders(lwd = 2, col = "blue") + # Customize line appearance
  tm_fill(col = "blue") + 
  tm_layout(frame = FALSE)
# 10. 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 (2').

Florida_counties<-counties(state = "Florida",cb=T)
## Retrieving data for the year 2021
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |======================================================================| 100%
Florida_counties<-st_transform(Florida_counties,crs = 4326)

Miami<-Florida_counties[Florida_counties$NAME=="Miami-Dade",]
Miami<-st_transform(Miami,crs = 4326) #define the coordinate reference system

tm_shape(Florida_counties) +
  tm_borders() +  # Add borders for polygons (customize as needed)
  tm_shape(MIA_Library) +
  tm_dots(size = 0.02, col = "red", alpha = 0.7) +  # Customize point appearance
  tm_basemap() +
  tm_layout(title = "Miami Libraries", frame = FALSE)
clipped_library_miami <- st_intersection(MIA_Library, Miami)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
tm_shape(Miami) +
  tm_borders() +  # Add borders for polygons (customize as needed)
  tm_shape(clipped_library_miami) +
  tm_dots(size = 0.02, col = "red", alpha = 0.7) +  # Customize point appearance
  tm_basemap() +
  tm_layout(title = "Miami Libraries", frame = FALSE)
Miami_ct <-tracts(state = "FL", county = "Miami-Dade",cb=T)
## Retrieving data for the year 2021
Miami_ct<-st_transform(Miami_ct,crs = 4326)

library_miami_ct <- st_intersection(clipped_library_miami, Miami_ct)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
# 11. Get Digital Elevation Model (DEM) data of a selected area of the county through this video

# 12. Read the DEM data and get the min and max of elevations (2').
library(raster)
## Loading required package: sp
DEM <- raster("/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Project 2/USGS_13_n26w081_20221103.tif")

DEM
## class      : RasterLayer 
## dimensions : 10812, 10812, 116899344  (nrow, ncol, ncell)
## resolution : 9.259259e-05, 9.259259e-05  (x, y)
## extent     : -81.00056, -79.99944, 24.99944, 26.00056  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=NAD83 +no_defs 
## source     : USGS_13_n26w081_20221103.tif 
## names      : USGS_13_n26w081_20221103
cellStats(DEM, min)
## [1] -7.604256
cellStats(DEM, max)
## [1] 31.30349
# 13. Make a map that shows areas with elevation lower than 350m (1').

col=terrain.colors(5)
brk <- c(0, 70, 140, 210, 280, 350)
plot(DEM, col=col, breaks=brk, main="DEM with more breaks")

# 14. Make a map that shows the slope degree of topography map is less than 3 (1').

DEM <- setMinMax(DEM)
cellStats(DEM, max)
## [1] 31.30349
plot(terrain(DEM, opt = "slope", unit = "degrees") < 3, 
     main = "Slope criteria",
     col = c("#ffffff", "#ff9900"))

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

# 16. Submit your code and result to Rpubs using R markdown (1').