-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.Rhistory
512 lines (512 loc) · 19.7 KB
/
.Rhistory
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#| crop: true
#| class-output: .hscroll
colnames(pm)
#| echo: true
#| eval: true
#| crop: true
#| class-output: .hscroll
# note the syntax here!
pm <- pm |>
select(siteid = `Site ID`, date, pm25 = `Daily Mean PM2.5 Concentration`,
AQI = `Daily AQI Value`, lon = `Site Longitude`, lat = `Site Latitude`)
pm
#| echo: true
#| eval: true
#| crop: true
#| class-output: .hscroll
pm <- vect(pm, geom = c("lon", "lat"), crs = "EPSG:4326")
pm
#| echo: false
#| eval: true
#| crop: true
#| fig-align: center
ga <- vect("week8files/tl_2020_13_tract.shp")
# make sure it's the same crs
ga <- project(ga, crs(pm))
ggplot() +
geom_spatvector(data = ga, lwd = 0.05, color = "gray", fill = NA) +
geom_spatvector(data = pm |> filter(month(date)==1), color = "red") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
#| echo: false
#| eval: true
#| crop: true
#| fig-align: center
# keep one observation per station
pmunique <- pm |>
group_by(siteid) |>
filter(row_number() == 1) |>
ungroup()
voronoiga <- voronoi(pmunique, bnd = ga)
ggplot() +
geom_spatvector(data = ga, lwd = 0.05, color = "gray", fill = NA) +
geom_spatvector(data = voronoiga, lwd = 0.05, color = "blue", fill = NA) +
geom_spatvector(data = pm |> filter(month(date)==1), color = "red") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
#| echo: true
#| eval: true
#| crop: true
#| fig-align: center
# keep one observation per station
pmmonth <- pm |>
filter(month(date)==1) |>
group_by(siteid) |>
summarize(pm25 = mean(pm25, na.rm = TRUE), .groups = "drop")
#| echo: true
#| eval: true
#| crop: true
#| fig-align: center
# Intersection
vorintersect <- intersect(ga, voronoiga)
# find area
vorintersect$area <- expanse(vorintersect)
# group by GEOID and find largest value
vorintersect <- vorintersect |>
group_by(GEOID) |>
filter(area==max(area)) |>
ungroup() |>
# now keep just the site id and geoid
select(GEOID, siteid) |>
as_tibble()
#| echo: false
#| eval: true
#| crop: true
#| fig-align: center
vorintersect <- vorintersect |>
left_join(as_tibble(pmmonth), by = "siteid")
gamonth <- ga |>
left_join(vorintersect, by = "GEOID")
ggplot() +
geom_spatvector(data = gamonth, aes(fill = pm25), color = NA) +
scale_fill_distiller("PM 2.5\n(Jan. 2020)", palette = "Spectral") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
#| echo: false
#| eval: true
#| crop: true
#| fig-align: center
# create centroids
gacentroids <- centroids(ga)
#intersect
vorcentroids <- intersect(gacentroids, voronoiga)
# create new gamonth
gamonth <- ga |>
mutate(siteid = vorcentroids$siteid)
gamonth <- gamonth |>
left_join(as_tibble(pmmonth), by = "siteid")
ggplot() +
geom_spatvector(data = gamonth, aes(fill = pm25), color = NA) +
scale_fill_distiller("PM 2.5\n(Jan. 2020)", palette = "Spectral") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
#| echo: true
#| eval: true
#| crop: true
#| fig-align: center
# new points
grid <- centroids(ga)
# Note: how I convert to sf inside the call!
# Note: idp is the "beta" from above
grid <- idw(pmmonth$pm25 ~ 1, sf::st_as_sf(pmmonth),
newdata = sf::st_as_sf(grid), idp = 1)
grid
#| echo: true
#| eval: true
#| crop: true
#| fig-align: center
gamonth$idwbeta1 <- grid$var1.pred
#| echo: false
#| eval: true
#| crop: true
#| fig-align: center
#| message: false
grid <- centroids(ga)
# Note: how I convert to sf inside the call!
# Note: idp is the "beta" from above
grid2 <- idw(pmmonth$pm25 ~ 1, sf::st_as_sf(pmmonth), newdata = sf::st_as_sf(grid), idp = 2)
gamonth$idwbeta2 <- grid2$var1.pred
# Note: how I convert to sf inside the call!
# Note: idp is the "beta" from above
grid3 <- idw(pmmonth$pm25 ~ 1, sf::st_as_sf(pmmonth), newdata = sf::st_as_sf(grid), idp = 3)
gamonth$idwbeta3 <- grid3$var1.pred
g1 <- ggplot() +
geom_spatvector(data = gamonth, aes(fill = idwbeta1), color = NA, show.legend = FALSE) +
scale_fill_distiller("PM 2.5\n(Jan. 2020)", palette = "Spectral") +
labs(subtitle = expression("Panel A: "~beta~" = 1")) +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
g2 <- ggplot() +
geom_spatvector(data = gamonth, aes(fill = idwbeta2), color = NA, show.legend = FALSE) +
scale_fill_distiller("PM 2.5\n(Jan. 2020)", palette = "Spectral") +
labs(subtitle = expression("Panel B: "~beta~" = 2")) +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
g3 <- ggplot() +
geom_spatvector(data = gamonth, aes(fill = idwbeta3), color = NA, show.legend = FALSE) +
scale_fill_distiller("PM 2.5\n(Jan. 2020)", palette = "Spectral") +
labs(subtitle = expression("Panel C: "~beta~" = 3")) +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
plot_grid(g1, g2, g3, ncol = 3) +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
#| echo: false
#| eval: true
#| crop: true
#| fig-align: center
#| message: false
grid <- centroids(ga)
grid <- idw(pmmonth$pm25 ~ 1, sf::st_as_sf(pmmonth), newdata = sf::st_as_sf(grid), nmax = 1)
gamonth$idwbetavor <- grid$var1.pred
ggplot() +
geom_spatvector(data = gamonth, aes(fill = idwbetavor), color = NA, show.legend = FALSE) +
scale_fill_distiller("PM 2.5\n(Jan. 2020)", palette = "Spectral") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
#| echo: false
#| eval: true
#| crop: true
#| fig-align: center
#| message: false
grid <- centroids(ga)
grid2 <- idw(pmmonth$pm25 ~ 1, sf::st_as_sf(pmmonth), newdata = sf::st_as_sf(grid), nmax = 2)
grid3 <- idw(pmmonth$pm25 ~ 1, sf::st_as_sf(pmmonth), newdata = sf::st_as_sf(grid), nmax = 10)
gamonth$idwbetavor2 <- grid2$var1.pred
gamonth$idwbetavor3 <- grid3$var1.pred
g1 <- ggplot() +
geom_spatvector(data = gamonth, aes(fill = idwbetavor), color = NA, show.legend = FALSE) +
scale_fill_distiller("PM 2.5\n(Jan. 2020)", palette = "Spectral") +
labs(subtitle = "Panel A: 1 neighbor") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
g2 <- ggplot() +
geom_spatvector(data = gamonth, aes(fill = idwbetavor2), color = NA, show.legend = FALSE) +
scale_fill_distiller("PM 2.5\n(Jan. 2020)", palette = "Spectral") +
labs(subtitle = "Panel B: 2 neighbors") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
g3 <- ggplot() +
geom_spatvector(data = gamonth, aes(fill = idwbetavor3), color = NA, show.legend = FALSE) +
scale_fill_distiller("PM 2.5\n(Jan. 2020)", palette = "Spectral") +
labs(subtitle = "Panel C: 10 neighbors") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
plot_grid(g1, g2, g3, ncol = 3) +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
library(rayshader)
g1 <- ggplot() +
geom_spatvector(data = gamonth, aes(fill = idwbeta1), color = NA) +
scale_fill_distiller("PM 2.5", palette = "Spectral") +
theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb"))
par(mfrow = c(1, 1))
plot_gg(g1, multicore = TRUE, theta = 10, phi = 55, shadow = FALSE, preview = TRUE, scale = 300, width = 7)
Sys.sleep(0.2)
render_snapshot()
devtools::install_github("h-a-graham/rayvista", dependencies=TRUE)
remotes::install_github(
"tylermorganwall/rayshader"
)
library(tidyverse)
library(terra)
library(tidyterra)
library(cowplot)
library(kableExtra)
kdisgreen <- "#006334"
accent <- "#340063"
accent2 <- "#633400"
kdisgray <- "#A7A9AC"
temp <- readRDS("/Users/Josh/Dropbox/Papers/coal_transformation/data/shrid2_clean_pc.RDS")
head(temp)
temp <- vect(temp)
temp
temp |> filter(substr(shrid2, 4, 5)=="02")
temp |> filter(substr(shrid2, 4, 5)=="03")
temp |> filter(substr(shrid2, 4, 5)=="01")
temp2 <- temp |> filter(substr(shrid2, 4, 5)=="01")
ggplot() + geom_spatvector(data = temp2)
temp2 <- temp |> filter(substr(shrid2, 4, 5)=="03")
ggplot() + geom_spatvector(data = temp2)
temp2 <- temp |> filter(substr(shrid2, 4, 5)=="05")
temp2
ggplot() + geom_spatvector(data = temp2)
temp2 <- temp |> filter(substr(shrid2, 4, 5)=="06")
temp2
ggplot() + geom_spatvector(data = temp2)
temp2 <- temp2[,c(1, 2, 9:ncol(temp2))]
temp2
colnames(temp2)
table(temp2$year)
names(temp2)
setwd("~/Dropbox/KDIS/Classes/geospatialdataR")
writeVector(temp2, "week9files/villages.shp")
#| label: setup
#| include: false
knitr::knit_hooks$set(crop = knitr::hook_pdfcrop)
library(tidyverse)
library(terra)
library(tidyterra)
library(cowplot)
library(kableExtra)
kdisgreen <- "#006334"
accent <- "#340063"
accent2 <- "#633400"
kdisgray <- "#A7A9AC"
# Haryana villages
villages <- vect("week9files/villages.shp")
villages
names(villages)
# let's look at pca_p_lit (literate population)
villages$literate <- villages$pca_p_lit/villages$pca_tot_p
summary(villages$literate)
table(villages$year)
villages <- villages |>
group_by(shrid2) |>
arrange(shrid2, year) |>
mutate(lit_lag = lag(literate)) |>
ungroup()
villages
#| label: setup
#| include: false
knitr::knit_hooks$set(crop = knitr::hook_pdfcrop)
library(tidyverse)
library(terra)
library(tidyterra)
library(cowplot)
library(kableExtra)
kdisgreen <- "#006334"
accent <- "#340063"
accent2 <- "#633400"
kdisgray <- "#A7A9AC"
theme_set(theme_bw() +
theme(plot.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")) +
theme(legend.background = element_rect(fill = "#f0f1eb", color = "#f0f1eb")))
# Haryana villages
#| eval: false
ggplot() +
geom_point(data = as_tibble(villages), aes(x = literate, y = lit_lag, color = year)) +
theme_bw()
#| eval: false
ggplot() +
geom_point(data = as_tibble(villages), aes(x = literate, y = lit_lag, color = as.factor(year))) +
theme_bw()
#| eval: false
ggplot() +
geom_point(data = as_tibble(villages) |> filter(year>1991), aes(x = literate, y = lit_lag, color = as.factor(year))) +
theme_bw()
#| eval: false
ggplot() +
geom_point(data = as_tibble(villages) |> filter(year>1991), aes(x = literate, y = lit_lag, color = as.factor(year))) +
scale_color_brewer("Year", palette = "Set1") +
theme_bw()
#| eval: false
ggplot() +
geom_point(data = as_tibble(villages) |> filter(year>1991), aes(x = literate, y = lit_lag, color = as.factor(year))) +
scale_color_brewer("Year") +
theme_bw()
#| eval: false
ggplot() +
geom_point(data = as_tibble(villages) |> filter(year>1991), aes(x = literate, y = lit_lag, color = as.factor(year))) +
scale_color_brewer("Year", palette = "Set2") +
theme_bw()
#| eval: false
ggplot() +
geom_point(data = as_tibble(villages) |> filter(year>1991),
aes(x = literate, y = lit_lag, color = as.factor(year))) +
scale_color_brewer("Year", palette = "Set2") +
labs(x = "Literacy", y = "Lagged literacy") +
geom_abline(intercept = 0, slope = 1, linetype = "dashed")
cor(villages$literate, villages$lit_lag, use = "pairwise.complete.obs")
mat <- adjacent(villages, symmetrical = TRUE)
villagessmall <- vect("week9files/villagessmall.shp")
# let's also just use 2001
villagessmall <- villagessmall[villagessmall$year==2001,]
villagessmall
mat <- adjacent(villagessmall, symmetrical = TRUE)
mat
p1 <- xy[mat[,1],]
points1 <- xy[mat[,1],]
xy <- centroids(villagessmall)
points1 <- xy[mat[,1],]
points2 <- xy[mat[,2],]
points1
ines(p1, p2, col='red', lwd=2)
lines(p1, p2, col='red', lwd=2)
lines(points1, points2, col='red', lwd=2)
xy <- sf::st_centroids(villagessmall)
xy <- sf::st_centroid(villagessmall)
xy <- centroids(villagessmall)
points1 <- geom(xy[mat[,1],])[,c("x", "y")]
points2 <- geom(xy[mat[,2],])[,c("x", "y")]
points1
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_line(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red")
xy <- centroids(villagessmall)
points1 <- geom(xy[mat[,1],])[,c("x", "y")]
points2 <- geom(xy[mat[,2],])[,c("x", "y")]
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_line(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red")
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_abline(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red")
points1 <- xy[mat[,1],]
points2 <- xy[mat[,2],]
points1 <- geom(xy[mat[,1],])[,c("x", "y")]
points2 <- geom(xy[mat[,2],])[,c("x", "y")]
ggplot() +
geom_abline(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red")
?geom_abline
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_abline(aes(xstart = points1$x, ystart = points1$y, xend = points2$x, yend = points2$y), color = "red")
ggplot() +
geom_abline(aes(xstart = points1$x, ystart = points1$y, xend = points2$x, yend = points2$y), color = "red")
ggplot() +
geom_spatvector(data = villagessmall, fill = NA)
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red")
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5)
ggplot() +
geom_abline(xstart = points1$x, ystart = points1$y, xend = points2$x, yend = points2$y, color = "red")
points1
ggplot() +
geom_abline(xstart = points1$x[1], ystart = points1$y[1], xend = points2$x[1], yend = points2$y[1], color = "red")
ggplot() +
geom_segment(data = coefmat, aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red")
ggplot() +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red")
points1
points1
points1$x
points1 <- as_tibble(geom(xy[mat[,1],])[,c("x", "y")])
points2 <- as_tibble(geom(xy[mat[,2],])[,c("x", "y")])
points1
points1$x
ggplot() +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red")
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red")
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red", size = 0.5)
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red", linewidth = 0.5)
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red", lwd = 0.5)
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red", lwd = 0.5)
villagessmall <- vect("week9files/villagessmall.shp")
# let's also just use 2001
villagessmall <- villagessmall[villagessmall$year==2001,]
mat <- adjacent(villagessmall, symmetrical = TRUE)
mat <- adjacent(villagessmall, symmetrical = TRUE)
head(mat)
xy <- centroids(villagessmall[villagessmall$year==2001,])
points1 <- as_tibble(geom(xy[mat[,1],])[,c("x", "y")])
points2 <- as_tibble(geom(xy[mat[,2],])[,c("x", "y")])
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red", lwd = 0.5)
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red", lwd = 0.1)
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red", lwd = 0.1) +
labs(x = "", y = "")
?adjacent
mat <- adjacent(villagessmall, symmetrical = TRUE, directions = "queen")
villagessmall <- vect("week9files/villagessmall.shp")
# let's also just use 2001
villagessmall <- villagessmall[villagessmall$year==2001,]
mat <- adjacent(villagessmall, symmetrical = TRUE, directions = "queen")
mat <- adjacent(villagessmall, symmetrical = TRUE, type = "queen")
mat <- adjacent(villagessmall, symmetrical = TRUE, type = "queen")
head(mat)
xy <- centroids(villagessmall[villagessmall$year==2001,])
points1 <- as_tibble(geom(xy[mat[,1],])[,c("x", "y")])
points2 <- as_tibble(geom(xy[mat[,2],])[,c("x", "y")])
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red", lwd = 0.1) +
labs(x = "", y = "")
villagessmall <- vect("week9files/villagessmall.shp")
# let's also just use 2001
villagessmall <- villagessmall[villagessmall$year==2001,]
mat <- adjacent(villagessmall, symmetrical = TRUE, type = "touches")
head(mat)
xy <- centroids(villagessmall[villagessmall$year==2001,])
points1 <- as_tibble(geom(xy[mat[,1],])[,c("x", "y")])
points2 <- as_tibble(geom(xy[mat[,2],])[,c("x", "y")])
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red", lwd = 0.1) +
labs(x = "", y = "")
villagessmall <- vect("week9files/villagessmall.shp")
# let's also just use 2001
villagessmall <- villagessmall[villagessmall$year==2001,]
mat <- adjacent(villagessmall, symmetrical = TRUE, type = "touches")
head(mat)
villagessmall <- vect("week9files/villagessmall.shp")
# let's also just use 2001
villagessmall <- villagessmall[villagessmall$year==2001,]
villagessmall <- vect("week9files/villagessmall.shp")
# let's also just use 2001
villagessmall <- villagessmall[villagessmall$year==2001,]
mat <- adjacent(villagessmall, symmetrical = TRUE, type = "touches")
head(mat)
xy <- centroids(villagessmall[villagessmall$year==2001,])
points1 <- as_tibble(geom(xy[mat[,1],])[,c("x", "y")])
points2 <- as_tibble(geom(xy[mat[,2],])[,c("x", "y")])
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = "red", size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = "red", lwd = 0.1) +
labs(x = "", y = "")
ggplot() +
geom_spatvector(data = villagessmall, fill = NA) +
geom_spatvector(data = xy, color = kdisgreen, size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = kdisgreen, lwd = 0.1) +
labs(x = "", y = "")
ggplot() +
geom_spatvector(data = villagessmall, fill = NA, color = kdisgray) +
geom_spatvector(data = xy, color = kdisgreen, size = 0.5) +
geom_segment(aes(x = points1$x, y = points1$y, xend = points2$x, yend = points2$y), color = kdisgreen, lwd = 0.1) +
labs(x = "", y = "")
mat <- adjacent(villagessmall, pairs = FALSE, type = "touches")
head(mat)
dim(mat)
villagessmall