-
Notifications
You must be signed in to change notification settings - Fork 4
/
live_311.Rmd
64 lines (56 loc) · 1.67 KB
/
live_311.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
---
title: "R Notebook"
# output: html_notebook
---
```{r}
library(dplyr)
library(lubridate)
library(ggplot2)
```
# API Call
```{r}
path_1 <- 'https://nycopendata.socrata.com/api/views/fhrw-4uyv/rows.csv?accessType=DOWNLOAD&query=select+*+where+%60created_date%60+%3E%3D+%27'
start_date <- '2018-04-05'
path_2 <- 'T00%3A00%3A00%27+AND+%60created_date%60+%3C+%27'
end_date <- '2018-04-07'
path_3 <- 'T23%3A59%3A59%27'
live_url <- paste(path_1, start_date, path_2, end_date, path_3, sep='')
live_data <- read.csv(live_url)
```
# Clean Data/Prepare
```{r}
live_data$Complaint.Date <- as.Date(as.POSIXct(live_data$Created.Date, format="%m/%d/%Y"))
live_data$Complaint.Hour <- hour(as.POSIXct(live_data$Created.Date, format="%m/%d/%Y %I:%M:%S %p"))
hour_range <- c(0:23)
live_data <- live_data[live_data$Complaint.Hour %in% hour_range, ]
```
# Save the map object locally
```{r}
# nyc_map <- qmap('New York City', zoom = 11)
# saveRDS(nyc_map, 'nyc_map.rds')
# nyc_map <- ggmap(readRDS('data/nyc_map.rds'))
```
# Filter by the top 15 most frequent complaints
```{r}
w = table(live_data$Complaint.Type)
t = as.data.frame(w)
names(t)[1] = 'Complaint.Type'
ct <- head(t[order(t$Freq, decreasing = TRUE),c(1,2)], 15)$Complaint.Type
df_complaints <- live_data[live_data$Complaint.Type %in% ct,]
# Select desired columns and remove NAs
df_live_select <- df_complaints[c('Complaint.Type','Longitude', 'Latitude')]
df_live_select <- na.omit(df_live_select)
```
# Plot
```{r}
# Plot
my_titles <- labs(
title = "Where Do Complaints Occur",
subtitle = '',
x = '',
y = ''
)
nyc_map +
geom_point(data = df_live_select, aes(x = Longitude, y = Latitude, colour = Complaint.Type), alpha=.5) +
my_titles
```