-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
291 lines (186 loc) · 6.6 KB
/
README.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
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# geoflowr
The goal of geoflowr is to ...
## Installation
You can get the most recent version of geoflowr from [Github](https://github.com/ropenscilabs/geoflowr) and install with:
``` r
devtools::install_github("ropenscilabs/geoflowr")
```
## The Problem
We are hoping to plot spatial flows using `ggplot2` and then using `gganimate` to show changes through time. There are many ways to do this ouside `ggplot2` and there is already a `geom_curve()` functionthat creates a plot similar
### Example Dataset
```{r data, fig.width=12, message=FALSE, warning=FALSE}
library(ggplot2)
library(readr)
library(purrr)
library(dplyr)
library(sf)
library(gganimate)
library(geosphere)
coords <- read_csv('data-raw/world.csv') %>%
filter(!is.na(admin)) %>%
select(admin,
continent,
x = Longitude,
y = Latitude)
#flows <- read_csv('data-raw/201802.csv') %>%
flows <- map_df(list.files(path = "data-raw", pattern = "[0-9].csv", full.names = TRUE),
read_csv, col_names = TRUE) %>%
select(year,
period,
reporter,
partner,
netweight_kg)
src <- coords %>%
rename(reporter = admin)
dst <- coords %>%
select(-continent) %>%
rename(partner = admin)
# join coords to src and dst
df <- flows %>%
left_join(src, by = 'reporter') %>%
left_join(dst, by = 'partner', suffix = c('_src','_dst')) %>%
filter(complete.cases(.),
partner != 'World')
world1 <- sf::st_as_sf(maps::map('world', plot = FALSE, fill = TRUE))
world2 <- sf::st_as_sf(maps::map('world', plot = FALSE, fill = TRUE, wrap = c(0,360)))
```
### Current ggplot implimentation - geom_curve()
```{r geom_curve, eval=FALSE, fig.width=12}
ggplot(df) +
geom_sf(data = world1) +
geom_curve(aes(x = x_src, y = y_src, xend = x_dst, yend = y_dst,
alpha = 0.5,
size = netweight_kg / max(df$netweight_kg)),
#curvature = 0.75, angle = -45,
arrow = arrow(length = unit(0.15,"cm"))
) +
theme_void() +
theme(legend.position="none") +
transition_time(period) +
ease_aes('linear')
```
![](images/first_attempt.gif)
### Convert Great Circle to points
```{r points}
#greatCircle(c(df$x_src[1], df$y_src[1]), c(df$x_dst[1], df$y_dst[1]), n=360, sp=FALSE)
# finter to single month for now
mar <- df %>%
as_tibble() %>%
filter(period == 201803)
mar_pts <- lapply(1:nrow(mar), function(r){
row = mar[r,]
pts <- gcIntermediate(c(row$x_src, row$y_src), c(row$x_dst, row$y_dst), n=36, sp=FALSE, breakAtDateLine = F) %>%
as.data.frame() %>%
mutate(year = row$year,
period = row$period,
group = r,
n = 1:36,
reporter = row$reporter,
partner = row$partner,
netweight_kg = row$netweight_kg)
}) %>% plyr::ldply()
```
```{r, eval=FALSE}
ggplot() +
geom_sf(data = world1) +
geom_line(data = mar_pts, aes(x = lon, y = lat, group = group)) +
theme_void() +
transition_reveal(id = group, along = n) +
ease_aes('linear')
```
![](images/third_attempt.gif)
### Convert to SF object
```{r sf, fig.width=11}
# https://www.jessesadler.com/post/great-circles-sp-sf/
mar_sf <- mar %>%
rowwise() %>%
mutate(line = st_sfc(st_linestring(gcIntermediate(c(x_src, y_src), c(x_dst, y_dst), n=36, sp=FALSE, breakAtDateLine = F), dim = 'XY'))) %>%
st_as_sf(sf_column_name = 'line', crs = 4283)
mapview::mapview(mar_sf)@map
```
```{r, eval=FALSE}
# wrld_wrap <- st_wrap_dateline(mar_sf, options = c("WRAPDATELINE=YES", "DATELINEOFFSET=180"),
# quiet = TRUE)
ggplot() +
geom_sf(data = world1) +
geom_sf(data = mar_sf) +
theme_void() +
transition_components('reporter') +
ease_aes('linear')
```
### Errors at Poles and Map borders
Using these methods great circle lines and point annimations appear distorted at the poles and the borders of the map. To avoid this we propose using destination and source points to draw a complete great circle. This line can then be split where the line is intersected resulting two lines. Each line can be checked for proximity to these map regions and excluded where nesecary.
The method is outlined below, but there is an issue with using `st_split()` on a windows machine.
```{r, fig.width=11}
src <- c(df$x_src[2], df$y_src[2])
dst <- c(df$x_dst[2], df$y_dst[2])
gc <- greatCircle(src, dst, n=16, sp=FALSE)
gci <- gcIntermediate(src, dst, n=16, sp=FALSE)
gc_l = st_sfc(st_linestring(gc), crs = 4283)
gci_l = st_sfc(st_linestring(gci), crs = 4283)
gc_long <- st_difference(gc_l, st_buffer(gci_l, 0.1))
mp <- st_sfc(st_multipoint(rbind(src,dst)), crs = 4283)
#mp2 = st_snap(mp, gcl, 1) # move points to intersect gcl line
# then use st_split(gcl, mp2) but requires lwgeom which is not working on windows....
ggplot() +
geom_sf(data = world1) +
geom_sf(data = gcl, size = 2.5) +
geom_sf(data = mp, colour = "red", fill = "red", size = 10) +
theme_void()
```
```{r, eval=FALSE}
longway <- function(src, dst){
pth <- gcIntermediate(src, dst, n=16, sp=FALSE, breakAtDateLine = F) # shortest path
if (any(abs(pth[,'lat']) > 88)) {
gc <- greatCircle(src, dst, n=16, sp=FALSE)
gc_l = st_sfc(st_linestring(gc), crs = 4283)
pth_l = st_sfc(st_linestring(pth), crs = 4283)
return(st_sfc(st_difference(gc_l, st_buffer(pth_l, 0.1))))
} else {
return(st_sfc(st_linestring(pth), crs = 4283))
}
}
mar_long <- mar %>%
rowwise() %>%
mutate(line = longway(src = c(x_src, y_src), dst = c(x_dst, y_dst))) %>%
st_as_sf(sf_column_name = 'line', crs = 4283)
mapview::mapview(mar_long)@map
```
### Project and Reproject Method
```{r}
longway2 <- function(src, dst){
srs <- '+proj=eqdc +lat_0=0 +lon_0=0 +lat_1=60 +lat_2=60 +x_0=0 +y_0=0 +a=6371000 +b=6371000 +units=m +no_defs'
names(src) <- names(dst) <- c('x', 'y')
rbind(src, dst) %>%
as.data.frame() %>%
st_as_sf(coords = c("x","y")) %>%
st_set_crs(4326) %>%
st_transform(srs) %>%
st_coordinates() %>%
st_linestring() %>%
st_segmentize(1e5) %>%
st_sfc(crs = srs) %>%
st_transform(4326)
}
mar_long2 <- mar %>%
rowwise() %>%
mutate(line = longway2(src = c(x_src, y_src), dst = c(x_dst, y_dst))) %>%
st_as_sf(sf_column_name = 'line')
ggplot() +
geom_sf(data = world1, size = 0.1) +
geom_sf(data = mar_long2, colour = 'darkred') +
#facet_wrap(~continent, ncol = 2) +
theme_void()
```