3 min read

Animating Growth of Allegheny County

In this post I will show how to create animated graphs that illustrate the increase in buildings in Allegheny County.

One caveat about the data: it only includes parcels that were sold at some point. If the parcel was not sold, it is not included in this data. For example, a structure that was torn down and replaced but was not sold is not included. It is also reasonable to assume that the data quality decreases the older the records are. There may be a large amount of missing data.

The shapefiles for the parcels come from Pennsylvania Spatial Data Access.

The data about the construction dates comes from the WPRDC’s Parcels n’at dashboard. To get the relevant data, draw a box around entire county, select the “Year Built” field in the Property Assessments section, and then download the data. It will take a while to download data for the entire county.

Set up the environment:

library(tidyverse)
library(janitor)
library(broom)
library(sf)
library(scales)
library(gganimate)
library(lwgeom)

options(scipen = 999, digits = 4)

theme_set(theme_bw())

my_caption <- "@conor_tompkins - data from @WPRDC"

This reads in data about the land parcel (lot lines):

df <- read_csv("parcel_data_allegheny_county.csv", progress = FALSE) %>% 
  clean_names() %>% 
  select(-geom)

This reads in the parcel geometry

file <- "shapefiles/AlleghenyCounty_Parcels201812.shx"
file
## [1] "shapefiles/AlleghenyCounty_Parcels201812.shx"
shapefile <- st_read(file)
## Reading layer `AlleghenyCounty_Parcels201812' from data source `C:\Users\conor\githubfolder\blog\content\post\shapefiles\AlleghenyCounty_Parcels201812.shx' using driver `ESRI Shapefile'
## Simple feature collection with 580948 features and 13 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 1241000 ymin: 321300 xmax: 1430000 ymax: 497900
## epsg (SRID):    NA
## proj4string:    +proj=lcc +lat_1=39.93333333333333 +lat_2=40.96666666666667 +lat_0=39.33333333333334 +lon_0=-77.75 +x_0=600000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs

Next we have to clean up the parcel geometry:

shapefile <- shapefile %>% 
  st_make_valid() %>% 
  clean_names() %>% 
  mutate(pin = as.character(pin))

Then, join the parcel geometry and parcel data:

parcel_data <- shapefile %>% 
  left_join(df)

This turns the parcel geometry into (x, y) coordinates:

centroids <- parcel_data %>% 
  st_centroid() %>% 
  st_coordinates() %>% 
  as_tibble() %>% 
  clean_names()

We can plot the coordinates to confirm that the locations make sense:

centroids %>% 
  distinct(x, y) %>% 
  ggplot(aes(x, y)) +
  geom_point() +
  theme_void() +
  coord_equal()

This plot shows that there is one row where yearblt_asmt is zero. That doesn’t make sense, so we will exclude it later.

df %>% 
  ggplot(aes(yearblt_asmt)) +
  geom_density() +
  geom_rug() +
  labs(title = "Structures in Allegheny County",
       x = "Year built",
       y = "Density",
       subtitle = my_caption)

This combines the parcel_data and centroid data:

parcel_geometry_cleaned <- bind_cols(parcel_data, centroids) %>% 
  select(pin, x, y, yearblt_asmt) %>%
  mutate(yearblt_asmt = as.integer(yearblt_asmt)) %>% 
  filter(!is.na(yearblt_asmt),
         yearblt_asmt > 1000) %>% 
  st_set_geometry(NULL)

This plots the culmulative sum of structures built:

parcel_cumulative <- parcel_geometry_cleaned %>% 
  select(pin, yearblt_asmt) %>% 
  arrange(yearblt_asmt) %>% 
  count(yearblt_asmt) %>% 
  mutate(cumulative_n = cumsum(n)) %>% 
  ggplot(aes(yearblt_asmt, cumulative_n)) +
  geom_line() +
  geom_point() +
  scale_y_continuous(label = comma) +
    labs(title = "Cumulative sum of structures built in Allegheny County",
       x = "Year Built",
       y = "Cumulative sum",
       caption = my_caption) +
  transition_reveal(yearblt_asmt)
parcel_cumulative

This creates a graph of the structures built in Allegheny County, colored by the construction year.

parcel_geometry_cleaned %>% 
  ggplot(aes(x, y, color = yearblt_asmt, group = pin)) +
  geom_point(alpha = .3, size = .1) +
  scale_color_viridis_c("Year structure was built") +
  theme_void() +
  theme(axis.text = element_blank(),
        axis.title = element_blank()) +
  labs(title = "Allegheny County land parcels",
       subtitle = "Year built: {frame_along}",
       caption = "@conor_tompkins, data from @WPRDC") +
  transition_reveal(yearblt_asmt)