Skip to content

Instantly share code, notes, and snippets.

@jonathanlxy
Last active July 25, 2016 15:23
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 jonathanlxy/cfffeca490683eb14912f2a53a2b7dc5 to your computer and use it in GitHub Desktop.
Save jonathanlxy/cfffeca490683eb14912f2a53a2b7dc5 to your computer and use it in GitHub Desktop.
Vision Zero Visualization Blog Post Gist
# Load NYC map
# Since this line actually run a query on Google Maps,
# You might want to save this map as RData for furture use
# If you don't know how to do it, please check save() and load() functions
# in the comment line below
nyc_map <- get_map(location = find_map_cent(mvc_count$LON,
mvc_count$LAT),
zoom = 11)
# save(nyc_map, file = 'nyc_map.RData')
# load('nyc_map.RData')
# Accident count by location
mvc_count <- work_dt %>%
filter(!(is.na(LATITUDE) | is.na(LONGITUDE))) %>%
mutate(LAT = round(LATITUDE, 2),
LON = round(LONGITUDE, 2)) %>%
group_by(LAT, LON) %>%
summarise(N = n())
# Top deaths by location
mvc_dth <- kill_dt %>%
filter(!(is.na(LATITUDE) | is.na(LONGITUDE))) %>%
filter(grepl('PERSONS', KILLED)) %>%
mutate(LAT = round(LATITUDE, 2),
LON = round(LONGITUDE, 2)) %>%
select(KILLED_V, LAT, LON) %>%
group_by(LAT, LON) %>%
summarise(KILLED_V = sum(KILLED_V)) %>%
arrange(desc(KILLED_V)) %>%
# table(mvc_sum$KILLED_V)
# 1 2 3 4 5 {} 6 7 8 10
# 231 121 50 17 13 {} 8 4 2 1
# ^ Break here
filter(KILLED_V >5)
# Mapping accident counts
ggmap(nyc_map, extent = 'device') +
geom_point(data = mvc_count, alpha = .5,
aes(x = LON, y = LAT, color = N, size = N)) +
scale_color_gradient(low = 'white', high = 'red') +
theme(legend.justification=c(1,1), legend.position=c(.95,.95),
legend.background = element_rect(color = 'black',
fill = alpha('white', 0.8)),
legend.title = element_blank()) +
guides(size = FALSE) +
ggtitle('Accident Frequencies by Location')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment