Skip to content

Instantly share code, notes, and snippets.

@elipousson
Last active July 21, 2022 02:18
Show Gist options
  • Save elipousson/331476e433d358c7db0d6f8abc8dc356 to your computer and use it in GitHub Desktop.
Save elipousson/331476e433d358c7db0d6f8abc8dc356 to your computer and use it in GitHub Desktop.
A R script to convert Maryland open data on notice of intent to foreclose by zipcode from a wide to a long format and create basic visualizations. Data: https://opendata.maryland.gov/Housing/Maryland-Notices-of-Intent-to-Foreclose-by-Zip-Cod/ftsr-vapt
library(dplyr)
library(ggplot2)
# pak::pkg_install("elipousson/mapmaryland")
plot_theme <- list(
hrbrthemes::theme_ipsum_pub(base_size = 15),
cols4all::scale_color_discrete_c4a_cat(palette = "tol.medium"),
cols4all::scale_fill_discrete_c4a_cat(palette = "tol.medium")
)
plot_caption <-
"Source: Maryland Office of the Commissioner of Financial Regulation (OCFR) /
Maryland Open Data\n
Plot by @elipousson"
noi_zip <-
mapmaryland::get_md_open_data(
resource = "ftsr-vapt"
)
md_zctas <-
mapmaryland::get_md_tigris(
type = "zctas",
year = 2010
)
md_counties <-
select(
mapmaryland::md_counties,
county_name = namelsad,
countyfp,
region
)
md_zctas <- sf::st_join(md_zctas, md_counties, largest = TRUE)
noi_zip <-
tidyr::pivot_longer(
noi_zip,
cols = starts_with("x"),
names_to = "date",
values_to = "count"
)
noi_zip_sf <-
mutate(
noi_zip,
date = stringr::str_remove_all(date, "x_|t00_00_00_000"),
date = lubridate::ymd(stringr::str_replace_all(date, "_", "-")),
date = dplyr::case_when(
date >= lubridate::ymd("2022-07-21") ~ as.Date(date - lubridate::dyears(1)),
TRUE ~ date
)
) %>%
left_join(
sf::st_drop_geometry(md_zctas),
by = c("zip" = "zcta5ce10")
)
ggplot(data = noi_zip_sf %>% filter(!is.na(county_name)), aes(x = date, y = as.integer(count), color = region)) +
geom_line(alpha = 0.3, aes(group = zip), size = 0.8) +
geom_point(alpha = 0.2, size = 1.6) +
scale_x_date(minor_breaks = NULL, date_labels = "%b '%y") +
labs(
x = "",
y = "Notices (#)",
title = "Notice of intent to foreclose (NOI) in Maryland by zipcode",
subtitle = "All regions of the state saw an increase between July '21 and June '22",
caption = plot_caption
) +
facet_wrap(~ region) +
guides(color = "none") +
plot_theme
noi_county_wide <-
mapmaryland::get_md_open_data(
resource = "w3bc-8mnv"
)
noi_county <-
noi_county_wide %>%
tidyr::pivot_longer(
cols = ends_with(c("county", "city", "blank")),
names_to = "county_name",
values_to = "count"
) %>%
mutate(
county_name = case_when(
county_name == "blank" ~ "Missing",
county_name == "baltimore_city" ~ "Baltimore city",
TRUE ~ stringr::str_to_title(stringr::str_replace(county_name, "_", " "))
),
date = lubridate::ymd(date),
count = as.integer(count)
) %>%
left_join(mapmaryland::md_counties %>% select(county_name = namelsad, region) %>% sf::st_drop_geometry())
noi_region <-
noi_county %>%
filter(region != "Missing county",
type == "Notice of Intent to Foreclose") %>%
group_by(date, region) %>%
summarise(
count = sum(as.integer(count))
)
ggplot(data = noi_region, aes(x = date, y = count, fill = region)) +
geom_col(alpha = 0.8) +
scale_x_date(minor_breaks = NULL, date_labels = "%b '%y") +
labs(
title = "Notices of intent to foreclose (NOI) in Maryland by region",
y = "Notices (#)",
x = "",
caption = plot_caption
) +
guides(fill = "none") +
facet_wrap(~ region) +
plot_theme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment