forked from info201b-w19/eviction-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.R
67 lines (55 loc) · 2.28 KB
/
analysis.R
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
# Analysis script: compute values and create graphics of interest
library("dplyr")
library("ggplot2")
library("lubridate")
library("tidyr")
library("ggmap")
# Load in your data
evictions <- read.csv("data/Eviction_Notices.csv", stringsAsFactors = FALSE)
# Compute some values of interest and store them in variables for the report
# How many evictions were there?
num_evictions <- nrow(evictions)
num_features <- ncol(evictions)
# Create a table (data frame) of evictions by zip code (sort descending)
by_zip <- evictions %>%
group_by(Eviction.Notice.Source.Zipcode) %>%
count() %>%
arrange(-n) %>%
ungroup() %>%
top_n(10, wt = n)
# Create a plot of the number of evictions each month in the dataset
by_month <- evictions %>%
mutate(date = as.Date(File.Date, format = "%m/%d/%y")) %>%
mutate(month = floor_date(date, unit = "month")) %>%
group_by(month) %>%
count()
# Store plot in a variable
month_plot <- ggplot(data = by_month) +
geom_line(mapping = aes(x = month, y = n), color = "blue", alpha = .5) +
labs(x = "Date", y = "Number of Evictions",
title = "Evictions over time in SF")
# Map evictions in 2017
evictions_2017 <- evictions %>%
mutate(date = as.Date(File.Date, format="%m/%d/%y")) %>%
filter(format(date, "%Y") == "2017") %>%
separate(Location, c("lat", "long"), ", ") %>% # split the column at the comma
mutate(
lat = as.numeric(gsub("\\(", "", lat)), # remove starting parentheses
long = as.numeric(gsub("\\)", "", long)) # remove closing parentheses
)
# Format the lat/long variables, filter to 2017
# Create a maptile background
base_plot <- qmplot(
data = evictions_2017, # name of the data frame
x = long, # data feature for longitude
y = lat, # data feature for latitude
geom = "blank", # don't display data points (yet)
maptype = "toner-background", # map tiles to query
darken = .7, # darken the map tiles
legend = "topleft" # location of legend on page
)
# Add a layer of points on top of the map tiles
evictions_plot <- base_plot +
geom_point(mapping = aes(x = long, y = lat), color = "red", alpha = .3) +
labs(title = "Evictions in San Francisco, 2017") +
theme(plot.margin = margin(.3, 0, 0, 0, "cm")) # adjust spacing around the map