Cleaned to change NULL to NA:
fc<-read.csv("/Users/hsmalley/Downloads/fallFood.csv",
head=TRUE,
stringsAsFactors = FALSE)
#str(fc)
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))
Variables for geography:
CensusTract
State
County
Variables of overall interest:
Pop2010
OHU2010
(total housing units)PovertyRate
MedianFamilyIncome
Flags: 0,1
Urban
GroupQuartersFlag
: Group quarters, tract with high share (\(>67\%\))LILATracts_1And10
: Flag for low-income and low access when considering low accessibilty at 1 and 10 milesHUNVFlag
: Flag for tract where \(>=\) 100 of households do not have a vehicle, and beyond 1/2 mile from supermarketLowIncomeTracts
: Flag for low income tractLA1and10
: Flag for low access tract at 1 mile for urban areas or 10 miles for rural areasLATracts1
: Flag for low access tract when considering 1 mile distanceLATracts10
: Flag for low access tract when considering 10 mile distanceIt 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:
LAPOP1_10
: Population count beyond 1 mile for urban areas or 10 miles for rural areas from supermarketLALOWI1_10
: Low income population count beyond 1 mile for urban areas or 10 miles for rural areas from supermarketVariables for 1 mile away from supermarkets: There are two columns for each one with counts and the other with percents.
lapop1(share)
: Low access, population at 1 mile, number(share)lalowi1(share)
: Low access, low-income population at 1 mile, number (share)lakids1(share)
: Low access, children age 0-17 at 1 mile, number (share)laseniors1(share)
: Low access, seniors age 65+ at 1 mile, number (share)lawhite1(share)
: Low access, White population at 1 mile, number (share)lablack1(share)
: Low access, Black or African American population at 1 mile, number (share)laasian1(share)
: Low access, Asian population at 1 mile, number (share)lanhopi1(share)
: Low access, Native Hawaiian and Other Pacific Islander population at 1 mile, number (share)laaian1(share)
: Low access, American Indian and Alaska Native population at 1 mile, number (share)laomultir1(share)
: Low access, Other/Multiple race population at 1 mile, number (share)lahisp1(share)
: Low access, Hispanic or Latino population at 1 mile, number (share)lahunv1(share)
: Vehicle access, housing units without and low access at 1 mile, number (share)lasnap1(share)
: Low access, housing units receiving SNAP benefits at 1 mile, number (share)Variables for 10 miles away from supermarkets: There are two columns for each one with counts and the other with percents.
lapop10(share)
: Low access, population at 10 miles, number(share)lalowi10(share)
: Low access, low-income population at 10 miles, number (share)lakids10(share)
: Low access, children age 0-17 at 10 miles, number (share)laseniors10(share)
: Low access, seniors age 65+ at 10 miles, number (share)lawhite10(share)
: Low access, White population at 10 miles, number (share)lablack10(share)
: Low access, Black or African American population at 10 miles, number (share)laasian10(share)
: Low access, Asian population at 10 miles, number (share)lanhopi10(share)
: Low access, Native Hawaiian and Other Pacific Islander population at 10 miles, number (share)laaian10(share)
: Low access, American Indian and Alaska Native population at 10 miles, number (share)laomultir10(share)
: Low access, Other/Multiple race population at 10 miles, number (share)lahisp10(share)
: Low access, Hispanic or Latino population at 10 miles, number (share)lahunv10(share)
: Vehicle access, housing units without and low access at 10 miles, number (share)lasnap10(share)
: Low access, housing units receiving SNAP benefits at 10 miles, number (share)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))
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
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'
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)