The first step in this process is to get the census data, which here will be the income data for North Carolina by county.
census_api_key("1fdbe7373f91fce952c471e37fa974cbe275d855")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
NCIncome <- get_acs(geography = "county",
state = "NC",
variables = "B17001_001",
year = 2020) %>%
select(GEOID, estimate) %>%
rename(CountyIncome = estimate)
## Getting data from the 2016-2020 5-year ACS
The next step is reading in the shape file from the computer, and setting it up to work with the census data, which requires a join.
nc_income <- st_read("C:/Users/alexi/Downloads/tl_2010_37_county10/tl_2010_37_county10.shp")
## Reading layer `tl_2010_37_county10' from data source
## `C:\Users\alexi\Downloads\tl_2010_37_county10\tl_2010_37_county10.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 100 features and 17 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: -84.32187 ymin: 33.75288 xmax: -75.40012 ymax: 36.58816
## Geodetic CRS: NAD83
summary(nc_income)
## STATEFP10 COUNTYFP10 COUNTYNS10 GEOID10
## Length:100 Length:100 Length:100 Length:100
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## NAME10 NAMELSAD10 LSAD10 CLASSFP10
## Length:100 Length:100 Length:100 Length:100
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## MTFCC10 CSAFP10 CBSAFP10 METDIVFP10
## Length:100 Length:100 Length:100 Length:100
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## FUNCSTAT10 ALAND10 AWATER10 INTPTLAT10
## Length:100 Min. :4.467e+08 Min. :3.673e+05 Length:100
## Class :character 1st Qu.:8.967e+08 1st Qu.:7.250e+06 Class :character
## Mode :character Median :1.198e+09 Median :1.608e+07 Mode :character
## Mean :1.259e+09 Mean :1.347e+08
## 3rd Qu.:1.500e+09 3rd Qu.:3.845e+07
## Max. :2.458e+09 Max. :3.054e+09
## INTPTLON10 geometry
## Length:100 POLYGON :100
## Class :character epsg:4269 : 0
## Mode :character +proj=long...: 0
##
##
##
nc_income2 <- mutate(nc_income, GEOID = GEOID10)
NCCData <- inner_join(NCIncome, nc_income2, by = "GEOID")
The final step is to make the map itself using geom_sf(). Here, the geometry is specified as well as the fill characteristic, as evidently converting something to a tibble breaks the geometry importing.
ggplot(NCCData) +
geom_sf(aes(fill = CountyIncome, geometry = geometry)) +
theme_minimal() +
scale_fill_fermenter(type = "seq", palette = "YlGn")
This gives us a map showing that, by county, North Carolina appears to be a relatively poor state, with some pockets of wealth. However, a flaw becomes apparant, which is that the highest category is in the millions. A glance at the census data itself shows that the wealthest county, Mecklenburg, is in the millions, whereas others are substantially lower. A way to potentially address this is to filter out the outliers.
NCCData2 <- NCCData %>%
filter(CountyIncome <= 500000)
#By doing this, we set a more reasonable limit, even though we will be excluding some data.
ggplot(NCCData2) +
geom_sf(aes(fill = CountyIncome, geometry = geometry)) +
theme_minimal() +
scale_fill_fermenter(type = "seq", palette = "YlGn")
This removed three counties, and reduced the scale to a more reasonable point. To look at specifically poorer counties, one more filter will be done.
NCCData3 <- NCCData %>%
filter(CountyIncome <= 250000)
ggplot(NCCData3) +
geom_sf(aes(fill = CountyIncome, geometry = geometry)) +
theme_minimal() +
scale_fill_fermenter(type = "seq", palette = "YlGn")
This filtered out 4 more counties, and shows a more reasonable map for seeing some degree of difference, as the first tier switches to second at $50,000.