For this assignment, I’ll be creating a map of Bexar county, divided into census tracts by median household income.
First, we load the data from ACS:
library(tidycensus)
library(tidyverse)
## -- Attaching packages ------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.3 v dplyr 1.0.2
## v tidyr 1.1.1 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts ---------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(ggplot2)
acs_variables <- load_variables(2017 , "acs5/profile", cache = TRUE)
#getting median income data
median_hsincome<-acs_variables %>% filter(name=="DP03_0062")
#narrowing focus to Bexar county
sa_acs<-get_acs(geography = "tract",
state="TX",
county = c("Bexar"),
year = 2017,
variables=c( "DP03_0062E") ,
geometry = T, output = "wide")
## Getting data from the 2013-2017 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using the ACS Data Profile
#rename variables and filter missing cases
sa_acs2<-sa_acs %>%
mutate(median_hs_income= DP03_0062E) %>%
na.omit()
now I’ll be using tmap and tmaptools to create the actual map:
library(tmap)
library(tmaptools)
tm_shape(sa_acs2)+
tm_polygons("median_hs_income", title="Bexar County Household Income", palette="Blues", style="quantile", n=5 ,legend.hist=T)+
tm_format("World", title="Median Household Income - Quantile Breaks", legend.outside=T)+
tm_scale_bar()+
tm_compass()
Above you’ll see the map as requested in the homework: Bexar County Median Household Income by census tract, with ledgend, title and scale.
Thank you!