library(tidyverse)
library(sf)
library(colorspace)
library(scales)
# useful on MacOS to speed up rendering of geom_sf() objects
if (!identical(getOption("bitmapType"), "cairo") && isTRUE(capabilities()[["cairo"]])) {
options(bitmapType = "cairo")
}
# set default theme
theme_set(theme_minimal())
# create reusable labels for each plot
<- labs(
map_labels title = "Median household income in New York in 2022",
subtitle = "By census tract",
color = NULL,
fill = NULL,
caption = "Source: American Community Survey"
)
Visualizing household income in New York
Packages
Load New York 2022 median household income
We will use two data files for this analysis. The first contains median household incomes for each census tract in New York from 2022. The second contains the boundaries of each county in New York.
# load data
<- read_rds(file = "data/ny-inc.rds")
ny_inc <- read_rds(file = "data/ny-counties.rds")
ny_counties
ny_inc
Simple feature collection with 5411 features and 4 fields (with 17 geometries empty)
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -79.76215 ymin: 40.4961 xmax: -71.85621 ymax: 45.01585
Geodetic CRS: NAD83
First 10 features:
GEOID NAME medincomeE
1 36015010800 Census Tract 108; Chemung County; New York 52713
2 36015000100 Census Tract 1; Chemung County; New York 38898
3 36015000300 Census Tract 3; Chemung County; New York NA
4 36055010200 Census Tract 102; Monroe County; New York 128860
5 36055011705 Census Tract 117.05; Monroe County; New York 97292
6 36055013902 Census Tract 139.02; Monroe County; New York 52242
7 36055004702 Census Tract 47.02; Monroe County; New York 43542
8 36055013604 Census Tract 136.04; Monroe County; New York 51081
9 36055006300 Census Tract 63; Monroe County; New York 61150
10 36055006000 Census Tract 60; Monroe County; New York 51341
medincomeM geometry
1 5419 MULTIPOLYGON (((-76.83044 4...
2 6329 MULTIPOLYGON (((-76.82196 4...
3 NA MULTIPOLYGON (((-76.83836 4...
4 19811 MULTIPOLYGON (((-77.61592 4...
5 30981 MULTIPOLYGON (((-77.48616 4...
6 15890 MULTIPOLYGON (((-77.65909 4...
7 5928 MULTIPOLYGON (((-77.61634 4...
8 18842 MULTIPOLYGON (((-77.69645 4...
9 34737 MULTIPOLYGON (((-77.64697 4...
10 9027 MULTIPOLYGON (((-77.56453 4...
ny_counties
Simple feature collection with 62 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -79.76215 ymin: 40.4961 xmax: -71.85621 ymax: 45.01585
Geodetic CRS: NAD83
First 10 features:
GEOID NAME medincomeE medincomeM
1 36067 Onondaga County, New York 71479 1125
2 36071 Orange County, New York 91806 1781
3 36017 Chenango County, New York 61741 2526
4 36053 Madison County, New York 68869 2781
5 36077 Otsego County, New York 65778 2967
6 36051 Livingston County, New York 70443 2811
7 36025 Delaware County, New York 58338 2143
8 36061 New York County, New York 99880 1781
9 36065 Oneida County, New York 66402 2173
10 36115 Washington County, New York 68703 2345
geometry
1 MULTIPOLYGON (((-76.49931 4...
2 MULTIPOLYGON (((-74.76247 4...
3 MULTIPOLYGON (((-75.88983 4...
4 MULTIPOLYGON (((-75.99243 4...
5 MULTIPOLYGON (((-75.41693 4...
6 MULTIPOLYGON (((-78.06075 4...
7 MULTIPOLYGON (((-75.42264 4...
8 MULTIPOLYGON (((-74.00641 4...
9 MULTIPOLYGON (((-75.88676 4...
10 MULTIPOLYGON (((-73.63622 4...
Draw a continuous choropleth of median household income
Your turn: Create a choropleth map of median household income in New York. Use a continuous color gradient to identify each tract’s median household income. Use a continuous color gradient to identify each tract’s median household income.
Use the stored map_labels
to set the title, subtitle, and caption for this and the remaining plots.
# add code here
Your turn: Now revise the map to use the Viridis color palette for improved readability.
# add code here
Overlay county borders
Your turn: To provide better context, overlay the NY county borders on the choropleth map.
# add code here
Convert the map into 6 discrete levels for median household income
Your turn: Continuous color palettes can be hard to distinguish visibly. To improve readability, convert the continuous color palette into a discrete one with 6 levels.
# add code here