Cleaning the Global environment and the console

rm(list = ls())      # Clears the global environment for a fresh start
cat('\f')            # Cleans the console

Set working directory

setwd("C:/Rizwan/Education/UofM PhD Work/Seminar in Earth Sciences/Exams/Midterm")
getwd()
## [1] "C:/Rizwan/Education/UofM PhD Work/Seminar in Earth Sciences/Exams/Midterm"

Loading libraries

This code allows to load multiple library at once.

my_library <- c("sf", "dplyr", "RColorBrewer", "ggplot2", "gridExtra", "classInt")
lapply(my_library, library, character.only = TRUE)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine

Unzipping the Data

unzip(zipfile = "midterm_data.zip", exdir = getwd())
ls()
## [1] "my_library"

Loading the data

dsn1 <- "Memphis_Demographics_GCS.shp"
dsn2 <- "Shelby.gdb"
MemDemog <- st_read(dsn1)
## Reading layer `Memphis_Demographics_GCS' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Memphis_Demographics_GCS.shp' using driver `ESRI Shapefile'
## Simple feature collection with 503 features and 4 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -90.31029 ymin: 34.99417 xmax: -89.73822 ymax: 35.26718
## geographic CRS: NAD83
Shelby <- st_read(dsn2)
## Multiple layers are present in data source C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb, reading layer `Community_Garden_shelby'.
## Use `st_layers' to list all layer names and their type in a data source.
## Set the `layer' argument in `st_read' to read a particular layer.
## Warning in evalq((function (..., call. = TRUE, immediate. = FALSE, noBreaks. =
## FALSE, : automatically selected the first layer in a data source containing more
## than one.
## Reading layer `Community_Garden_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 33 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 756844.4 ymin: 298726.4 xmax: 794158.8 ymax: 341723.2
## projected CRS:  NAD83 / Tennessee (ftUS)
Data <- as_tibble(read.table("fips_demographics.txt", header = TRUE, sep = ","))
head(Data)
## # A tibble: 6 x 18
##   OBJECTID STATE_FIPS BLKGRP    FIPS POP2010 POP2013 WHITE BLACK MALES FEMALES
##      <int>      <int>  <int>   <dbl>   <int>   <int> <int> <int> <int>   <int>
## 1        1         47      1 4.72e11     999    1007    75   871   456     543
## 2        2         47      4 4.72e11     698     683    68   318   357     341
## 3        3         47      1 4.72e11    1203    1063   116  1035   531     672
## 4        4         47      1 4.72e11     293     293    80   213    40     253
## 5        5         47      2 4.72e11     862     859   520   186   414     448
## 6        6         47      5 4.72e11    1101    1135   674   210   570     531
## # ... with 8 more variables: MED_AGE <dbl>, AVE_FAM_SZ <dbl>, HSE_UNITS <int>,
## #   OWNER_OCC <int>, RENTER_OCC <int>, GlobalID <chr>, Shape__Area <dbl>,
## #   Shape__Length <dbl>

Read all layers from the geodatabase

LayerList <- st_layers(dsn2)
LayerList
## Driver: OpenFileGDB 
## Available layers:
##                   layer_name     geometry_type features fields
## 1    Community_Garden_shelby             Point       33     17
## 2              School_shelby             Point      391     17
## 3                Rail_shelby Multi Line String      339      8
## 4               Parks_shelby     Multi Polygon      226      6
## 5             Library_shelby             Point       22     16
## 6     Law_Enforcement_shelby             Point       37     17
## 7            Hospital_shelby             Point       21     17
## 8      Health_Centers_shelby             Point       31     16
## 9              Grocer_shelby             Point       60     16
## 10               Fire_shelby             Point       89     17
## 11      Farmer_Market_shelby             Point       14     17
## 12             County_shelby     Multi Polygon        1     23
## 13              roads_shelby Multi Line String     2763      8
## 14 Memphis_City_Demographics     Multi Polygon      503     19
class(LayerList)
## [1] "sf_layers"
LayerList[1]
## $name
##  [1] "Community_Garden_shelby"   "School_shelby"            
##  [3] "Rail_shelby"               "Parks_shelby"             
##  [5] "Library_shelby"            "Law_Enforcement_shelby"   
##  [7] "Hospital_shelby"           "Health_Centers_shelby"    
##  [9] "Grocer_shelby"             "Fire_shelby"              
## [11] "Farmer_Market_shelby"      "County_shelby"            
## [13] "roads_shelby"              "Memphis_City_Demographics"

Question 1

Name = LayerList$name
Layers <- lapply(c(1:length(Name)), function(i) { 
  st_read(dsn2, layer = Name[i])
})
## Reading layer `Community_Garden_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 33 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 756844.4 ymin: 298726.4 xmax: 794158.8 ymax: 341723.2
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `School_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 391 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 734398 ymin: 263306.1 xmax: 876914 ymax: 397840.4
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Rail_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 339 features and 8 fields
## geometry type:  MULTILINESTRING
## dimension:      XY
## bbox:           xmin: 727063.6 ymin: 263436.6 xmax: 883773.4 ymax: 407636.1
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Parks_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 226 features and 6 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: 732044.5 ymin: 265123.9 xmax: 878833.9 ymax: 410431.8
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Library_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 22 features and 16 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 752812.9 ymin: 272549.6 xmax: 875503.7 ymax: 390129.8
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Law_Enforcement_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 37 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 752117.2 ymin: 278903.1 xmax: 872274.7 ymax: 390481
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Hospital_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 21 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 760308.5 ymin: 279451.1 xmax: 856890.3 ymax: 351008.6
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Health_Centers_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 31 features and 16 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 757687.9 ymin: 277327.6 xmax: 872210.3 ymax: 391961
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Grocer_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 60 features and 16 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 755399 ymin: 271441.9 xmax: 875874.3 ymax: 401002.8
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Fire_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 89 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 739899.8 ymin: 269570.5 xmax: 873174.7 ymax: 392902.6
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Farmer_Market_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 14 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 754547.3 ymin: 279243.4 xmax: 875874.3 ymax: 390219.8
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `County_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 1 feature and 23 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 677966.9 ymin: 261279.3 xmax: 885736.8 ymax: 412664.8
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `roads_shelby' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 2763 features and 8 fields
## geometry type:  MULTILINESTRING
## dimension:      XY
## bbox:           xmin: -90.12757 ymin: 34.99427 xmax: -89.6358 ymax: 35.40568
## geographic CRS: WGS 84
## Reading layer `Memphis_City_Demographics' from data source `C:\Rizwan\Education\UofM PhD Work\Seminar in Earth Sciences\Exams\Midterm\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 503 features and 19 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 677979 ymin: 263221.8 xmax: 850942.3 ymax: 364390.3
## projected CRS:  NAD83 / Tennessee (ftUS)
names(Layers) <- Name
Schools <- Layers$School_shelby
Libraries <- Layers$Library_shelby
libBuff_2km <- st_buffer(Libraries, 6561.68) # Multiplied by 6561.68 to convert from feet to kilometers
libBuff_intersec <- st_intersection(libBuff_2km, Schools)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
nSchools <- libBuff_intersec %>% group_by(Library_Na) %>% summarise(n_Schools=n()) %>% filter(n_Schools>=10)
## `summarise()` ungrouping output (override with `.groups` argument)
plot(st_geometry(Libraries), axes = T, main = "Schools within 2KM of the Libraries", xlab = "Easting (ft)", ylab = "Northing (ft)", cex.main = 1.5)
plot(st_geometry(libBuff_2km), add = T, border = "red")
plot(st_geometry(libBuff_intersec), add = T, col = "blue")

cat("Number of schools exists within 2 km of bufferzones is: ", sum(nSchools$n_Schools), sep = '')
## Number of schools exists within 2 km of bufferzones is: 56

The list of the Libraries having most schools within 2 km bufferzone.

head(nSchools)
## Simple feature collection with 5 features and 2 fields
## geometry type:  MULTIPOINT
## dimension:      XY
## bbox:           xmin: 750390.5 ymin: 300350.1 xmax: 807441.6 ymax: 329737.9
## projected CRS:  NAD83 / Tennessee (ftUS)
## # A tibble: 5 x 3
##   Library_Na            n_Schools                                          Shape
##   <chr>                     <int>                  <MULTIPOINT [US_survey_foot]>
## 1 Cornelia Crenshaw Br~        12 ((757511.5 321188.3), (757738.8 317564), (758~
## 2 Gaston Park Branch L~        12 ((750390.5 309804.1), (752022.2 306007.4), (7~
## 3 North Branch Library         11 ((762900.2 326318), (763058.3 325328.8), (763~
## 4 Poplar White Station~        10 ((798720.7 309226.4), (800709.8 308824), (801~
## 5 Randolph Branch Libr~        11 ((787640 326204.7), (788289.8 321280.7), (789~

Question 2

Checking the projection systems

Before joining to layers, it is necessary to check their projection systems. We used st_crs to check the projection systems.

st_crs(MemDemog)
## Coordinate Reference System:
##   User input: NAD83 
##   wkt:
## GEOGCRS["NAD83",
##     DATUM["North American Datum 1983",
##         ELLIPSOID["GRS 1980",6378137,298.257222101,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["latitude",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["longitude",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     ID["EPSG",4269]]
st_crs(Schools)
## Coordinate Reference System:
##   User input: NAD83 / Tennessee (ftUS) 
##   wkt:
## PROJCRS["NAD83 / Tennessee (ftUS)",
##     BASEGEOGCRS["NAD83",
##         DATUM["North American Datum 1983",
##             ELLIPSOID["GRS 1980",6378137,298.257222101,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4269]],
##     CONVERSION["SPCS83 Tennessee zone (US Survey feet)",
##         METHOD["Lambert Conic Conformal (2SP)",
##             ID["EPSG",9802]],
##         PARAMETER["Latitude of false origin",34.3333333333333,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8821]],
##         PARAMETER["Longitude of false origin",-86,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8822]],
##         PARAMETER["Latitude of 1st standard parallel",36.4166666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8823]],
##         PARAMETER["Latitude of 2nd standard parallel",35.25,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8824]],
##         PARAMETER["Easting at false origin",1968500,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8826]],
##         PARAMETER["Northing at false origin",0,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8827]]],
##     CS[Cartesian,2],
##         AXIS["easting (X)",east,
##             ORDER[1],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##         AXIS["northing (Y)",north,
##             ORDER[2],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["USA - Tennessee"],
##         BBOX[34.98,-90.31,36.68,-81.65]],
##     ID["EPSG",2274]]
st_crs(Libraries)
## Coordinate Reference System:
##   User input: NAD83 / Tennessee (ftUS) 
##   wkt:
## PROJCRS["NAD83 / Tennessee (ftUS)",
##     BASEGEOGCRS["NAD83",
##         DATUM["North American Datum 1983",
##             ELLIPSOID["GRS 1980",6378137,298.257222101,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4269]],
##     CONVERSION["SPCS83 Tennessee zone (US Survey feet)",
##         METHOD["Lambert Conic Conformal (2SP)",
##             ID["EPSG",9802]],
##         PARAMETER["Latitude of false origin",34.3333333333333,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8821]],
##         PARAMETER["Longitude of false origin",-86,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8822]],
##         PARAMETER["Latitude of 1st standard parallel",36.4166666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8823]],
##         PARAMETER["Latitude of 2nd standard parallel",35.25,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8824]],
##         PARAMETER["Easting at false origin",1968500,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8826]],
##         PARAMETER["Northing at false origin",0,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8827]]],
##     CS[Cartesian,2],
##         AXIS["easting (X)",east,
##             ORDER[1],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##         AXIS["northing (Y)",north,
##             ORDER[2],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["USA - Tennessee"],
##         BBOX[34.98,-90.31,36.68,-81.65]],
##     ID["EPSG",2274]]

Reprojecting the shape file

MemDemog_ftUS <- st_transform(MemDemog, st_crs(Schools))
st_crs(MemDemog_ftUS)
## Coordinate Reference System:
##   User input: NAD83 / Tennessee (ftUS) 
##   wkt:
## PROJCRS["NAD83 / Tennessee (ftUS)",
##     BASEGEOGCRS["NAD83",
##         DATUM["North American Datum 1983",
##             ELLIPSOID["GRS 1980",6378137,298.257222101,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4269]],
##     CONVERSION["SPCS83 Tennessee zone (US Survey feet)",
##         METHOD["Lambert Conic Conformal (2SP)",
##             ID["EPSG",9802]],
##         PARAMETER["Latitude of false origin",34.3333333333333,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8821]],
##         PARAMETER["Longitude of false origin",-86,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8822]],
##         PARAMETER["Latitude of 1st standard parallel",36.4166666666667,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8823]],
##         PARAMETER["Latitude of 2nd standard parallel",35.25,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8824]],
##         PARAMETER["Easting at false origin",1968500,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8826]],
##         PARAMETER["Northing at false origin",0,
##             LENGTHUNIT["US survey foot",0.304800609601219],
##             ID["EPSG",8827]]],
##     CS[Cartesian,2],
##         AXIS["easting (X)",east,
##             ORDER[1],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##         AXIS["northing (Y)",north,
##             ORDER[2],
##             LENGTHUNIT["US survey foot",0.304800609601219]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["USA - Tennessee"],
##         BBOX[34.98,-90.31,36.68,-81.65]],
##     ID["EPSG",2274]]

Calculating population growth, defining breaks, and ploting the data

MemDemog_ftUS_merged <- merge(MemDemog_ftUS,Data, by.x = "FIPS", by.y = "FIPS")
Pop_Growth <- MemDemog_ftUS_merged %>%
  group_by(FIPS) %>% 
  summarise(Pop2010 = POP2010,
            Pop2013 = POP2013,
            rate = (Pop2013 - Pop2010)/Pop2010)
## `summarise()` ungrouping output (override with `.groups` argument)
breaks_qt <- classIntervals(c(min(Pop_Growth$rate)-0.00001, 
                              Pop_Growth$rate), n = 7, style = "quantile")
## Warning in classIntervals(c(min(Pop_Growth$rate) - 1e-05, Pop_Growth$rate), :
## var has missing values, omitted in finding classes
breaks_qt
## style: quantile
##   [-0.1869436,-0.04643758)  [-0.04643758,-0.02499779) 
##                         72                         71 
## [-0.02499779,-0.008012499) [-0.008012499,0.001669955) 
##                         72                         71 
##   [0.001669955,0.01529958)    [0.01529958,0.03988599) 
##                         72                         71 
##     [0.03988599,0.2933151] 
##                         72
Pop_Growth <- mutate(Pop_Growth, growth_rate_cat = cut(rate, breaks_qt$brks))
ggplot(Pop_Growth)+
  geom_sf(aes(fill=growth_rate_cat))+
  scale_fill_brewer(palette = "YlOrRd")