#This lab will explore the spatial distribution of climate damages across the US and the relationship between climate damages and areas with a high percentage of disadvantaged residents.
#CEJST’s basic spatial unit is the 2010 census tract. Tracts are largish neighborhoods averaging about 4,000 people. They are relatively stable over time. They are nested within counties in the same way the counties are nested within states.
#Tracts are identified by an 11-character geoid that consists of fips codes for state (2 characters), county (3 characters) and tract (6 characters). This means the first two characters of the code identify the tract’s state, and the first five characters of the code identify the tract’s county. This make it very easy for R programmers to group_by() and summarize() tract-level data to states or counties.
suppressWarnings({library(tidyverse)
library(readxl)
library(sf)
library(RColorBrewer)
library(tmap)
library(plotly)
library(here)
})
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
##
##
## Attaching package: 'plotly'
##
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
##
## The following object is masked from 'package:stats':
##
## filter
##
##
## The following object is masked from 'package:graphics':
##
## layout
##
##
## here() starts at C:/temp/class2322/_B__Classes_export (2)/class2322/ClimateImpacts
source("functions.R")
j1 <- read_excel("communities-2022-05-31-1915GMT.xlsx")
j2 <- j1 %>%
tolow() %>%
mutate(geocode = paste0("g", mid(census.tract.id,1,5))) %>%
select(geocode,census.tract.id,everything()) %>%
mutate(totpop = total.population,
dispop = ifelse(identified.as.disadvantaged=="TRUE",
total.population,0))
j3 <- j2 %>%
group_by(geocode) %>%
summarize(totpop=sum(totpop,na.rm=T),
dispop = sum(dispop,na.rm=T)) %>%
mutate(dispct = 100*dispop/totpop) %>%
mutate(disadv = ifelse(dispct > 50,TRUE,FALSE))
gacejst = readRDS("gacejst.rds") %>%
# the two commands starting with "disadv =" are not a mistake.
# the first sets every tract to 0, even those with missing values.
# the second sets disadvantaged tracts to 1.
# the variable sm_c is a binary 1/0 variable representing whether
# or not the tract is disadvantaged.
mutate(disadv = 0,
disadv = ifelse(sm_c == 1, 1, disadv),
geocode = paste0("g",geoid10)) %>%
select(geocode,disadv,everything())
#The following tmap code draws Georgia cejst tracts in an interactive map.
#Zoom to the Georgia Tech area and identify disadvantaged tracts near campus.
tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap("OpenStreetMap") +
tm_shape(gacejst) +
tm_polygons("disadv",alpha=0.5, palette="Reds")