-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.qmd
406 lines (350 loc) · 11.7 KB
/
README.qmd
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
---
title: netvis
# format: html
format: gfm
number-sections: true
toc: true
editor:
render-on-save: true
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
warning = FALSE,
message = FALSE,
cache = TRUE
)
library(tidyverse)
library(tmap)
tmap_mode("view")
```
<!-- badges: start -->
<!-- badges: end -->
The goal of this repo is to demonstrate different visualisation techniques for communicating information about transport networks.
```{r, eval = FALSE, echo=FALSE}
# install.packages("devtools")
# devtools::install_github("ITSLeeds/netvis")
library(tidyverse)
```
# Example data
We'll use a route network dataset from the Propensity to Cycle Tool (PCT) to demonstrate the package.
The PCT is a web application that allows users to explore cycling potential across England and Wales.
The PCT is available at [www.pct.bike](https://www.pct.bike/).
We also provide an example dataset from Limerick, Ireland, from the CRUSE project.
This is imported and plotted with code shown below.
```{r}
#| eval: false
#| echo: false
u = "https://cruse.bike/limerick/rnet_combined_balanced.gpkg"
rnet_limerick_balanced_full = sf::read_sf(u)
# Make numeric cols numerics not chars:
# To do this, values of "1-10" need to be changed to 3 (or another integer)
rnet_limerick_balanced_full = rnet_limerick_balanced_full |>
mutate(across(starts_with("Bic"), as.numeric)) |>
mutate(across(starts_with("Bic"), replace_na, replace = 3))
zones_limerick = zonebuilder::zb_zone("limerick")
central_limerick = zones_limerick |>
dplyr::filter(circle_id == 1) |>
sf::st_union()
rnet_limerick = sf::st_intersection(rnet_limerick_balanced_full, central_limerick)
rnet_limerick = rnet_limerick |>
mutate(across(c(`Bicycle (Baseline)`:`Bicycle (Ebike)`), ~as.numeric(.)))
sf::st_write(rnet_limerick, "test-data/rnet_limerick.geojson", delete_dsn = TRUE)
usethis::use_data(rnet_limerick)
```
```{r}
rnet_limerick = sf::read_sf("test-data/rnet_limerick.geojson")
plot(rnet_limerick)
```
# Static data visualisation
A simple visualisation of the data in a multi-panel static map is shown below.
```{r}
#| eval: false
#| echo: false
rnet_leeds = pct::get_pct_rnet("west-yorkshire")
rnet_leeds = rnet_leeds |>
select(-1)
zones_leeds = zonebuilder::zb_zone("Leeds")
central_leeds = zones_leeds |>
dplyr::filter(circle_id == 1) |>
sf::st_union()
rnet_central = sf::st_intersection(rnet_leeds, central_leeds)
# sf::st_write(rnet_central, "test-data/rnet_central.geojson", delete_dsn = TRUE)
rnet_minimal = rnet_central |>
slice_max(ebike_slc, n = 2) |>
mutate(across(where(is.numeric), function(x) x * 1:2))
rnet_minimal
usethis::use_data(rnet_minimal, overwrite = TRUE)
```
Read in data for Leeds with the following:
```{r}
rnet_central = sf::read_sf("test-data/rnet_central.geojson")
plot(rnet_central)
usethis::use_data(rnet_central, overwrite = TRUE)
```
In Python this would look something like this:
```{python}
import geopandas as gpd
rnet_central = gpd.read_file("test-data/rnet_central.geojson")
rnet_central.plot()
```
# Interactive maps
Interactive maps are more engaging and policy-relevant, allowing people to zoom in and explore results.
The images below illustrate interactive maps of the route network data focussed on the `bicycle` and `dutch_slc` variables.
```{r, echo=FALSE}
rnet_central = rnet_central |>
select(bicycle, dutch_slc)
m1 = tm_shape(rnet_central) +
tm_lines(
lwd = "bicycle",
scale = 9
)
tmap_save(m1, "maps/m1.html")
# browseURL("maps/m1.html")
# webshot2::webshot("maps/m1.html")
width_multiplier = round(max(rnet_central$dutch_slc) / max(rnet_central$bicycle))
```
The maximum level of flow in the Go Dutch scenario is `r round(width_multiplier)` times the maximum level of flow in the baseline scenario.
We can use this to scale the line widths as illustrated below.
```{r}
m2 = tm_shape(rnet_central) +
tm_lines(
lwd = "dutch_slc",
scale = 9 * width_multiplier
)
tmap_save(m2, "maps/m2.html")
# browseURL("maps/m2.html")
if(!file.exists("maps/m1.png")) {
webshot2::webshot("maps/m1.html", "maps/m1.png")
webshot2::webshot("maps/m2.html", "maps/m2.png")
}
```
::: {#m1-2 layout-ncol="2"}
![](maps/m1.png){#fig-surus}
![](maps/m2.png){#fig-hanno}
:::
There are two problems with line widths in the maps shown above:
- The thinnes lines are too thin
- The thickest lines are too thick
```{r, echo=FALSE}
max_width = 15
width_multiplier = 5
```
Given that the maximum width is determined by the `scale` argument, we can solve the first problem by increasing the value of `scale`.
The second problem can be solved with a multiplier that prevents lines being x times thicker than the thinnest lines, `r width_multiplier` times in this case.
```{r, echo=FALSE}
# get 95th percentile of line widths
combined_values = rnet_central |>
sf::st_drop_geometry() |>
sapply(function(x) {
quantile(x, 0.95)
}
)
minimum_value_allowed = max(combined_values) / width_multiplier
```
```{r, echo=FALSE, out.width="50%", fig.show="hold"}
scale_bicycle = max_width / (quantile(rnet_central$dutch_slc, 0.95) / quantile(rnet_central$bicycle, 0.95))
# summary(rnet_central$bicycle)
rnet_bicycle = rnet_central |>
mutate(lwd = case_when(
bicycle < minimum_value_allowed ~ minimum_value_allowed,
TRUE ~ bicycle
))
# summary(rnet_bicycle$lwd)
m3 = tm_shape(
rnet_bicycle
) +
tm_lines(
lwd = "lwd",
scale = scale_bicycle
)
# tmap_save(m3, "maps/m3.html")
# browseURL("maps/m3.html")
# For the Go Dutch scenario
scale_dutch = max_width
m4 = tm_shape(
rnet_central |>
mutate(lwd = case_when(
dutch_slc < minimum_value_allowed ~ minimum_value_allowed,
TRUE ~ dutch_slc
))
) +
tm_lines(
lwd = "lwd",
scale = scale_dutch
)
# tmap_save(m4, "maps/m4.html")
# browseURL("maps/m4.html")
if(!file.exists("maps/m3.png")) {
webshot2::webshot("maps/m3.html", "maps/m3.png")
webshot2::webshot("maps/m4.html", "maps/m4.png")
}
```
::: {layout-ncol="2"}
![](maps/m3.png)
![](maps/m4.png)
:::
A function that does this would look something like this:
```{r}
scale_line_widths = function(x, max_width, min_width) {
# ...
}
```
In the plot below, for example, we increase the thickness of the thinnest lines by setting `min_width` to 3, making the lines with less flow more visible.
```{r, echo=FALSE}
x = rnet_central |>
select(bicycle, dutch_slc)
scale_line_widths = function(
x,
max_width = 15,
min_width = 1,
ptile = 0.99,
width_regex = "bicycle|dutch_slc",
width_multiplier = NULL
) {
names_width = names(x)[grepl(width_regex, names(x))]
names_width_lwd = paste0(names_width, "_lwd")
names(names_width_lwd) = names_width
if(is.null(width_multiplier)) {
width_multiplier = max_width / min_width
}
x_no_outliers = x |>
sf::st_drop_geometry() |>
select(matches(width_regex)) |>
mutate_all(function(x) {
x[x > quantile(x, ptile)] = quantile(x, ptile)
x
})
# waldo::compare(x, x_no_outliers) # large values gone
max_value = max(x_no_outliers)
x_normalized = x_no_outliers |>
mutate_all(function(x) x / max_value)
# summary(x_normalized)
minimum_value_allowed = 1 / width_multiplier
x_scaled = x_normalized |>
mutate_all(function(x) case_when(
x < minimum_value_allowed ~ minimum_value_allowed,
TRUE ~ x
))
summary(x_scaled)
maximums = x_scaled |> sapply(max)
max_widths = maximums * max_width
names(x_scaled) = names_width_lwd
x_to_plot = cbind(x, x_scaled)
nm = names_width[1]
map_list = lapply(names_width, function(nm) {
tm_shape(x_to_plot) +
tm_lines(
lwd = names_width_lwd[nm],
scale = max_widths[[nm]]
)
})
# map_list[[1]]
map_list
}
```
```{r}
m5 = scale_line_widths(x, max_width = 15, min_width = 3)
# m5[[1]]
# m5[[2]]
```
```{r, echo=FALSE, out.width="50%", fig.show="hold"}
tmap_save(m5[[1]], "maps/m5.html")
# browseURL("maps/m5.html")
# webshot2::webshot("maps/m5.html", "maps/m5.png")
tmap_save(m5[[2]], "maps/m6.html")
# browseURL("maps/m6.html")
# webshot2::webshot("maps/m6.html", "maps/m6.png")
```
::: {layout-ncol="2"}
![](maps/m5.png)
![](maps/m6.png)
:::
Instead of showing different layers side-by-side, it's useful to be able to select them as different layers interactively.
The `netvis()` function defined in this packages does that, as illustrated below.
![](https://user-images.githubusercontent.com/1825120/253802338-83fdcbea-3a43-4ae7-b2d6-db6810e9c89c.png)
<!-- Python attempt: -->
```{python}
#| eval: false
#| echo: false
def scale_line_widths(
data,
baseline_column,
dutch_column,
max_width = 15,
width_multiplier = 5
):
"""
Scale line widths for interactive maps.
Parameters
----------
data : pandas.DataFrame
Dataframe containing the data to be plotted.
baseline_column : str
Name of the column containing the baseline values.
dutch_column : str
Name of the column containing the Go Dutch values.
max_width : int, optional
Maximum width of the lines in the interactive map. The default is 15.
width_multiplier : int, optional
The minimum width of the lines is the maximum value in the baseline
data divided by this value. The default is 5.
Returns
-------
data : pandas.DataFrame
Dataframe with a new column containing the scaled line widths.
"""
# get 95th percentile of line widths
combined_values = data[[baseline_column, dutch_column]].quantile(0.95)
# Set mininum value to the maximum value in the combined data divided by the minimum width multiplier
minumum_value_allowed = combined_values.max() / width_multiplier
# scale line widths
scale = max_width / (combined_values[dutch_column] / combined_values[baseline_column])
data["lwd_dutch"] = data[dutch_column].apply(lambda x: minimum_value_allowed if x < minimum_value_allowed else x)
data["lwd_bicycle"] = data[baseline_column].apply(lambda x: minimum_value_allowed if x < minimum_value_allowed else x)
return data, scale
```
<!-- Starting with the data in `test-data/rnet_central.geojson` let's test this function on a small subset of the data. -->
```{python}
#| eval: false
#| echo: false
import pandas as pd
import geopandas as gpd
import numpy as np
data = gpd.read_file("test-data/rnet_central.geojson")
data = data[["bicycle", "dutch_slc"]]
data = data.iloc[0:10, :]
dw, scale = scale_line_widths(data, "bicycle", "dutch_slc")
print(dw)
```
```{r}
#| eval: false
#| echo: false
# https://github.com/nptscot/outputdata/releases/tag/v2023-07-11-00-00-48.247456_commit_282d4661618e5e60ad9c34a6368db84745473d18
rnet_edinburgh = readRDS("/tmp/combined_network.Rds")
sf::write_sf(rnet_edinburgh, "~/github/nptscot/networkmerge/large_route_network_example_edingurgh.geojson")
zones_edinburgh = zonebuilder::zb_zone("princes street edinburgh")
mapview::mapview(zones_edinburgh)
# library(osmdata)
# princes_street_all = opq("edinburgh") %>%
# add_osm_feature(key = "name", value = "Princes Street") %>%
# osmdata_sf()
osm_edinburgh = osmextract::oe_get("Edinburgh")
princes_street = osm_edinburgh |>
filter(name == "Princes Street")
plot(princes_street$geometry)
princes_street_buffer = princes_street |>
sf::st_union() |>
sf::st_buffer(dist = 100)
rnet_princes_street = rnet_edinburgh[princes_street_buffer, , op = sf::st_within]
plot(rnet_princes_street$geometry)
rnet_princes_street = rnet_princes_street |>
select(commute_fastest_bicycle_go_dutch, Quietness)
rnet_princes_street |> plot()
sf::write_sf(rnet_princes_street, "data/rnet_princes_street_minimal.geojson")
sf::write_sf(rnet_princes_street, "~/github/nptscot/networkmerge/data/rnet_princes_street_minimal.geojson")
```