Skip to content

Instantly share code, notes, and snippets.

@erikgregorywebb
Created February 13, 2019 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikgregorywebb/fcdd5ee54e9ef0672a03f0b2807a675c to your computer and use it in GitHub Desktop.
Save erikgregorywebb/fcdd5ee54e9ef0672a03f0b2807a675c to your computer and use it in GitHub Desktop.
library(tidyverse)
library(gganimate)
library(gifski)
library(png)
library(scales)
# import
inventory = read.csv('https://raw.githubusercontent.com/erikgregorywebb/datasets/master/rentalInventory_All.csv', stringsAsFactors = F)
inventory = inventory %>% gather('Month', 'Inventory', -areaName, -Borough, -areaType)
# clean
inventory$Year = substr(sub('X', '', inventory$Month), 1, 4)
inventory$Month = substr(sub('X', '', inventory$Month), 6, 8)
inventory$Date = as.Date(paste(inventory$Year, inventory$Month, '01', sep = '-'))
# 1. Inventory by Borough Over Time
p1 = inventory %>%
filter(areaType == 'borough') %>%
filter(Borough != 'Staten Island') %>%
ggplot(aes(Date, Inventory, group = areaName)) +
geom_line(color='steelblue', size = 1) +
geom_segment(aes(xend = as.Date('2019-01-01', format = '%Y-%m-%d'), yend = Inventory), linetype = 2, colour = 'grey') +
geom_point(color='steelblue', size = 1) +
geom_text(aes(x = as.Date('2019-01-01', format = '%Y-%m-%d'), label = areaName), hjust = 0, size = 6) +
scale_y_continuous(label = comma) +
scale_x_date(limits = c(as.Date('2010-01-01', format = '%Y-%m-%d'), as.Date('2019-05-01', format = '%Y-%m-%d'))) +
transition_reveal(Date) +
coord_cartesian(clip = 'off') +
labs(title = 'NYC Rental Inventory by Borough',
subtitle = 'Source: StreetEasy, 2010-18',
y = 'Avaliable Units') +
theme_minimal() +
theme(text = element_text(size = 18)) +
theme(plot.title = element_text(size = 24)) +
theme(plot.margin = margin(5.5, 50, 5.5, 5.5))
anim_save("p1.gif", p1, width = 1000, height = 650)
# 2. Average Monthly Inventory by Borough
p2 = inventory %>%
filter(areaType == 'borough') %>%
filter(Borough != 'Staten Island') %>%
group_by(Borough, Month) %>%
summarise(avgInventory = mean(Inventory)) %>%
mutate(Month = as.numeric(Month)) %>%
ggplot(aes(Month, avgInventory, group = Borough)) +
geom_line(color='steelblue', size = 1) +
geom_segment(aes(xend = 12, yend = avgInventory), linetype = 2, colour = 'grey') +
geom_point(color='steelblue', size = 1) +
geom_text(aes(x = 12, label = Borough), hjust = 0, size = 6) +
scale_y_continuous(label = comma) +
scale_x_discrete(limits=1:12, labels = month.abb) +
transition_reveal(Month) +
coord_cartesian(clip = 'off') +
labs(title = 'Average NYC Monthly Inventory by Borough',
subtitle = 'Source: StreetEasy, 2010-18',
y = 'Avaliable Units') +
theme_minimal() +
theme(text = element_text(size = 18)) +
theme(plot.title = element_text(size = 24)) +
theme(plot.margin = margin(5.5, 50, 5.5, 5.5))
anim_save("p2.gif", p2, width = 1000, height = 650)
# 3. Rental Inventory in Brooklyn's Neighborhoods
p3 = inventory %>%
filter(areaType == 'neighborhood',
Borough == 'Brooklyn') %>%
ggplot(aes(Date, Inventory, group = areaName)) +
geom_line(color='steelblue', size = 1) +
geom_point(color='steelblue', size = 1) +
facet_wrap(~areaName) +
theme(axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank()) +
transition_reveal(Date) +
coord_cartesian(clip = 'off') +
labs(title = 'Rental Inventory in Brooklyn Neighborhoods',
subtitle = 'Source: StreetEasy, 2010-18',
y = 'Avaliable Units') +
theme_minimal() +
theme(text = element_text(size = 18)) +
theme(plot.title = element_text(size = 24)) +
theme(plot.margin = margin(5.5, 50, 5.5, 5.5))
anim_save("p3.gif", p3, width = 1000, height = 650)
# Note: animate(p1, fps = 12, renderer = gifski_renderer(loop = TRUE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment