1) Load Food Insecurity Data

Cleaned to change NULL to NA:

fc<-read.csv("/Users/hsmalley/Downloads/fallFood.csv", 
             head=TRUE, 
             stringsAsFactors = FALSE)

#str(fc)

2) Filter for State

This filters the data for Oregon and also creates a variable called GEOID to join with spatial data later.

GEOID is a code that is used by the US Census Bureau to identify geographic areas (that uses FIPS codes).

(https://www.census.gov/programs-surveys/geography/guidance/geo-identifiers.html)

The GEOID can be seperated into digits:

library(tidyverse)

or<-fc%>%
  filter(State=="Oregon")%>%
  mutate(GEOID=as.numeric(CensusTract))

3) Variables of Interest

Variables for geography:

Variables of overall interest:

Flags: 0,1

It appears that there are different definitions of low access to supermarkets (food insecurity) depending on whether a given census tract is designated at Urban or Rural.

Possible response variables:

Variables for 1 mile away from supermarkets: There are two columns for each one with counts and the other with percents.

Variables for 10 miles away from supermarkets: There are two columns for each one with counts and the other with percents.

4) Make a map

A) Download shape files from the Census Bureau (External Data)

You will need an API Key.

#install.packages("tidycensus")
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 3.6.2
## To install your API key for use in future sessions, run this function with `install = TRUE`.

Select the year of interest and state. You can also select data from that American Community Survey (ACS). The variable B25077_001E is for median home value, for example.

# Set a year of interest
# It looks like 2019 is the most recent year of data
this.year = 2019

# MEDIAN HOME VALUE with Geometry
orMedvG <- get_acs(geography = "tract", year=this.year,
                   state = "OR", 
                   variables = "B25077_001E", 
                   geometry = TRUE)%>%
  mutate(GEOID=as.numeric(GEOID))

B) Join the shape file (sf) and the data frame (df) using tigris

library(tigris)
## Warning: package 'tigris' was built under R version 3.6.2
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
## USE GEO_JOIN TO COMBINE SPATIAL DATA AND OTHER DATA FRAMES
joinOR<-geo_join(spatial_data=orMedvG , data_frame=or, 
                 by_sp='GEOID', by_df='GEOID')
## Warning: `group_by_()` was deprecated in dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help

C) Make a reactive map with leaflet

library(leaflet)

## CREATE A POPUP MESSAGE
popup<-paste("Tract: ", as.character(substring(joinOR$GEOID, 6, 11)), "<br>",
             "Population Low Access: ", as.character(joinOR$LAPOP1_10))

### QUANTILE COLORS
qpal<-colorQuantile("viridis", domain=joinOR$LAPOP1_10,
                    n=5,na.color="#FFFFFF")

leaflet()%>%
  addProviderTiles("CartoDB.Positron")%>%
  addPolygons(data=joinOR,
              fillColor= ~qpal(joinOR$LAPOP1_10),
              fillOpacity = 0.7,
              color="grey",
              opacity=.5,
              weight = 0.4,
              smoothFactor = 0.2,
              popup = popup)%>%
  addLegend("bottomright", pal=qpal, values=joinOR$LAPOP1_10,
            opacity = .7,
            title="Percentiles")
## Warning: sf layer has inconsistent datum (+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs ).
## Need '+proj=longlat +datum=WGS84'

5) Look at Demographics for a tract

this.tract<-"41037960100"

tractT<-fc%>%
  filter(CensusTract==as.numeric(this.tract))

#dim(tractT)

##### DEMOGRAPHICS

### ONE MILE

dem1<-data.frame(Demographics=c("White", 
                                "Black", 
                                "Asian", 
                                "Native Hawaiian and \nOther Pacific Islander", 
                                "American Indian and \nAlaska Native", 
                                "Other/Multiple Race", 
                                "Hispanic"),
                 Counts=c(tractT$lawhite1, 
                          tractT$lablack1, 
                          tractT$laasian1, 
                          tractT$lanhopi1, 
                          tractT$laaian1, 
                          tractT$laomultir1, 
                          tractT$lahisp1), 
                 Percents=c(tractT$lawhite1share, 
                            tractT$lablack1share, 
                            tractT$laasian1share, 
                            tractT$lanhopi1share, 
                            tractT$laaian1share, 
                            tractT$laomultir1share, 
                            tractT$lahisp1share))



### TEN MILES
dem10<-data.frame(Demographics=c("White", 
                                "Black", 
                                "Asian", 
                                "Native Hawaiian and \nOther Pacific Islander", 
                                "American Indian and \nAlaska Native", 
                                "Other/Multiple Race", 
                                "Hispanic"),
                 Counts=c(tractT$lawhite10, 
                          tractT$lablack10, 
                          tractT$laasian10, 
                          tractT$lanhopi10, 
                          tractT$laaian10, 
                          tractT$laomultir10, 
                          tractT$lahisp10), 
                 Percents=c(tractT$lawhite10share, 
                            tractT$lablack10share, 
                            tractT$laasian10share, 
                            tractT$lanhopi10share, 
                            tractT$laaian10share, 
                            tractT$laomultir10share, 
                            tractT$lahisp10share))


### SELECT A DISTANCE
this.distance = 10

if(this.distance==1){
  this.dem.df<-dem1
  title<-paste("Share of tract population by race beyond ", 
               this.distance,
               " mile from supermarket", sep="")
}
if(this.distance==10){
  this.dem.df<-dem10
  title<-paste("Share of tract population by race beyond ", 
               this.distance,
               " miles from supermarket", sep="")
  
}

this.dem.df$Demographics<-factor(this.dem.df$Demographics, 
                                 levels = c("White",
                                            "Black",
                                            "Asian",
                                            "Native Hawaiian and \nOther Pacific Islander", 
                                            "American Indian and \nAlaska Native", 
                                            "Other/Multiple Race",
                                            "Hispanic"))



ggplot(this.dem.df, aes(Percents, Demographics))+
  geom_col()+
  scale_y_discrete("",limits=rev)+
  theme_bw()+
  xlim(c(0, 100))+ # NOTE ONLY USE THIS LIMIT IF PERCENT
  geom_text(aes(label = Percents), hjust = -0.2)+
  ggtitle(title)