-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmelbourne_bar_viz.R
179 lines (131 loc) · 6.02 KB
/
melbourne_bar_viz.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
###########################################################################
#
# Title: # Melbourne bar vizualisation with leaflet and R
#
# Author: Julien-Yacine Chaqra
# Date: 07/01/2018
#
###########################################################################
# Packages ----------------------------------------------------------------
# install.packages("rgdal")
# devtools::install_github('rstudio/leaflet')
library(htmlwidgets)
library(magrittr)
library(deldir)
library(rgdal)
library(mapproj)
library(dplyr)
library(leaflet)
library(sp)
library(ggplot2)
library(KernSmooth)
library(geosphere)
library(tableHTML)
library(plotly)
# Data --------------------------------------------------------------------
# https://www.data.gouv.fr/fr/datasets/bars-pubs-et-brasseries-artisanales-dopen-beer-map-ile-de-france-mai-2015/
# https://github.com/Guts/Paris-Beer-Week/blob/master/data/raw_data/getOpenBeerMap.py
## Update the data from the overpass api -----------------------------------
# execute_py_script <- paste0("python ", file.path(getwd(),"scrape_data.py"))
# system(execute_py_script)
# Import and vizualise ----------------------------------------------------
# Melbourne
biere <- rgdal::readOGR("./melbourne_bar_viz.geojson") %>%
as.data.frame() %>%
mutate(name = as.character(NAME), OSM_ID = as.character(OSM_ID)) %>%
filter(!is.na(name)) %>%
select(.,OSM_ID, name, coords.x1, coords.x2) %>%
rename(longitude = coords.x1, latitude = coords.x2) %>%
distinct(longitude, latitude, .keep_all = TRUE)
# Simple vizualisation
melbourne_bar_basic_map <- leaflet() %>%
addTiles() %>%
setView(lat = -37.814, lng = 144.96332 , zoom = 12) %>%
addCircles(lng = biere$longitude, lat = biere$latitude, radius = 1) %>%
addCircleMarkers(lng = biere$longitude, lat = biere$latitude, popup = biere$name, radius = 2)
melbourne_bar_basic_map
# saveWidget(widget = melbourne_bar_basic_map, file = "./melbourne_bar_basic_map.html")
# Voronoi diagram ---------------------------------------------------------
# Source
# http://flowingdata.com/2016/04/12/voronoi-diagram-and-delaunay-triangulation-in-r/
# https://rud.is/b/2015/07/26/making-staticinteractive-voronoi-map-layers-in-ggplotleaflet/
vor_pts <- SpatialPointsDataFrame(cbind(biere$longitude,
biere$latitude),
biere, match.ID=TRUE)
# bar with duplicated coordinates ?
# biere[duplicated(biere[,c("longitude", "latitude")]),]
SPointsDF_to_voronoi_SPolysDF <- function(sp) {
# tile.list extracts the polygon data from the deldir computation
vor_desc <- tile.list(deldir(sp@coords[,1], sp@coords[,2]))
lapply(1:(length(vor_desc)), function(i) {
# tile.list gets us the points for the polygons but we
# still have to close them, hence the need for the rbind
tmp <- cbind(vor_desc[[i]]$x, vor_desc[[i]]$y)
tmp <- rbind(tmp, tmp[1,])
# now we can make the Polygon(s)
Polygons(list(Polygon(tmp)), ID=i)
}) -> vor_polygons
# hopefully the caller passed in good metadata!
sp_dat <- sp@data
# this way the IDs _should_ match up w/the data & voronoi polys
rownames(sp_dat) <- sapply(slot(SpatialPolygons(vor_polygons),
'polygons'),
slot, 'ID')
SpatialPolygonsDataFrame(SpatialPolygons(vor_polygons),
data=sp_dat)
}
vor <- SPointsDF_to_voronoi_SPolysDF(sp = vor_pts)
# Voronoï
melbourne_bar_voronoi_map <- leaflet() %>%
addTiles() %>%
setView(lat = -37.814, lng = 144.96332 , zoom = 12) %>%
addPolygons(data = vor, color = "#FF1E90", fillColor = "transparent", weight = 2) %>%
addCircles(lng = biere$longitude, lat = biere$latitude, radius = 1) %>%
addCircleMarkers(lng = biere$longitude, lat = biere$latitude, popup = biere$name, radius = 2)
melbourne_bar_voronoi_map
# saveWidget(widget = melbourne_bar_voronoi_map, file = "./melbourne_bar_voronoi_map.html")
# density heatmap -------------------------------------------------------------
# Source:
# https://gis.stackexchange.com/questions/168886/r-how-to-build-heatmap-with-the-leaflet-package
kde <- biere %>% select(., longitude, latitude) %>%
bkde2D(., bandwidth=c(.0045, .0068), gridsize = c(75,75))
CL <- contourLines(kde$x1 , kde$x2 , kde$fhat)
## EXTRACT CONTOUR LINE LEVELS
LEVS <- as.factor(sapply(CL, `[[`, "level"))
NLEV <- length(levels(LEVS))
## CONVERT CONTOUR LINES TO POLYGONS
pgons <- lapply(1:length(CL), function(i)
Polygons(list(Polygon(cbind(CL[[i]]$x, CL[[i]]$y))), ID=i))
spgons = SpatialPolygons(pgons)
melbourne_bar_density_map <- leaflet() %>%
addTiles() %>%
setView(lat = -37.814, lng = 144.96332 , zoom = 12) %>%
addPolygons(data = spgons, color = heat.colors(n = NLEV, alpha = NULL)[LEVS]) %>%
addCircles(lng = biere$longitude, lat = biere$latitude, radius = 1) %>%
addCircleMarkers(lng = biere$longitude, lat = biere$latitude, popup = biere$name, radius = 2)
melbourne_bar_density_map
# saveWidget(widget = melbourne_bar_density_map, file = "./melbourne_bar_density_map.html")
# Counting the number of bar in a radius of 20, 100 and 500 meters --------
# Sometimes you wanna go from one bar to another
# google: "R number of point in a radius"
biere_arround <- cbind(biere, sapply(c(20, 100, 500), function(x){
rowSums(geosphere::distm(biere[, c("longitude", "latitude")], fun = distHaversine) < x)
}) %>% set_colnames(c("m_20", "m_100", "m_500"))) %>% arrange(desc(m_20), desc(m_100), desc(m_500))
ggplot(data = biere_arround, aes(x = m_20)) +
geom_bar() +
scale_x_continuous(breaks = 0:3) +
ggtitle("Number of bars in a radius of 20 meters") +
xlab("Number of bars") +
ylab("Count")
ggplot(data = biere_arround, aes(x = m_100)) +
geom_bar() +
scale_x_continuous(breaks = 0:10) +
ggtitle("Number of bars in a radius of 100 meters") +
xlab("Number of bars") +
ylab("Count")
ggplot(data = biere_arround, aes(x = m_500)) +
geom_bar() +
scale_x_continuous(breaks = seq(0, 60, 5)) +
ggtitle("Number of bars in a radius of 500 meters") +
xlab("Number of bars") +
ylab("Count")