/
Housing-stock.Rmd
104 lines (65 loc) · 2.45 KB
/
Housing-stock.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
---
title: "Mass. housing stock"
author: "Gabriel Florit"
output:
html_document:
self_contained: false
---
The following analysis looks at declining middle-class housing stock in Massachusetts.
On the format: each question is followed by the [R](https://www.r-project.org/) code that generates the answer. This is also known as **reproducible research**, a practice that's slowly being adopted by newspapers (e.g. [538](https://github.com/fivethirtyeight/data), [The Upshot.](https://github.com/theupshot)) From [wikipedia](http://en.wikipedia.org/wiki/Reproducibility#Reproducible_research): "The term reproducible research refers to the idea that the ultimate product of academic research is the paper along with the full computational environment used to produce the results in the paper such as the code, data, etc. that can be used to reproduce the results and create new work based on the research."
```{r, message=F, warning=F, echo=F}
# set working directory
setwd("~/Documents/dev/housing-stock")
# load libraries
library(stringr)
library(tidyr)
library(dplyr)
library(ggplot2)
library(plotflow)
data <- read.csv('./housing-stock.csv') %>%
rename(town=Municipality) %>%
mutate(
may_2016 = single_2016 + condo_2016,
may_2012 = (single_2012 + condo_2012) - may_2016,
town = str_to_title(town)
)
```
***
###Show me the change in total units for sale for all of Eastern Mass.:
```{r, fig.height=1.5}
data %>%
select(town, may_2016, may_2012) %>%
gather(date, count, -town) %>%
group_by(date) %>%
summarise(count = sum(count)) %>%
mutate(town = c('MA', 'MA')) %>%
arrange(desc(date)) %>%
ggplot(aes(town, count, fill=date)) +
geom_bar(stat='identity', position='stack') +
coord_flip() +
scale_y_continuous(labels=scales::comma) +
labs(
title='Eastern Mass. units for sale in May',
subtitle='For properties under $500,000'
) +
xlab('') + ylab('Units for sale')
```
***
###Show me the change in total units for sale for each town in Eastern Mass.:
```{r, fig.height=20, warning=F}
data %>%
select(town, may_2016, may_2012) %>%
gather(date, count, -town) %>%
group_by(town, date) %>%
summarise(count = sum(count)) %>%
arrange(desc(date)) %>%
ggplot(aes(town, count, fill=date)) +
geom_bar(stat='identity', position='stack') +
coord_flip() +
scale_y_continuous(labels=scales::comma) +
labs(
title='Eastern Mass. units for sale in May',
subtitle='For properties under $500,000'
) +
xlab('') + ylab('Units for sale')
```