forked from HugoBlox/theme-academic-cv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 27.5 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
goal=locations[2], theta = 0.01)
print(paste((j/nrow(Pj_combn))*100, "% complete"))
if(length(passages) == nrow(Pj_combn)){
passages_layer <- stack(passages)
passage_layers[[paste(layers[i])]] <- sum(passages_layer)/nrow(Pj_combn)
}
}
}
}
passage_layers
1:nlayers(vars$elv)
vars_tr
length(vars_tr) == nlayers(vars)
vars_tr <- list()
passages <- list()
passage_layers <- list()
for(i in 1:nlayers(vars$elv)){
vars_tr[[paste(layers[i])]] <- transition(vars[[i]], transitionFunction = mean, 4) %>%
geoCorrection(type="c",multpl=F)
if(length(vars_tr) == nlayers(vars$elv)){
print("Completed transition layers")
for (j in 1:nrow(Pj_combn)){
locations <- SpatialPoints(rbind(Pj_coords[Pj_combn[j,1],1:2],
Pj_coords[Pj_combn[j,2],1:2]),
crs(Pj_asp))
passages[[j]] <- passage(vars_tr[[i]],
origin=locations[1],
goal=locations[2], theta = 0.01)
print(paste((j/nrow(Pj_combn))*100, "% complete"))
if(length(passages) == nrow(Pj_combn)){
passages_layer <- stack(passages)
passage_layers[[paste(layers[i])]] <- sum(passages_layer)/nrow(Pj_combn)
}
}
}
}
passage_layers
length(passages) == nrow(Pj_combn)
vars_tr <- list()
passages <- list()
passage_layers <- list()
for(i in 1:nlayers(vars)){
vars_tr[[paste(layers[i])]] <- transition(vars[[i]], transitionFunction = mean, 4) %>%
geoCorrection(type="c",multpl=F)
if(length(vars_tr) == nlayers(vars)){
print("Completed transition layers")
for (j in 1:nrow(Pj_combn)){
locations <- SpatialPoints(rbind(Pj_coords[Pj_combn[j,1],1:2],
Pj_coords[Pj_combn[j,2],1:2]),
crs(Pj_asp))
passages[[j]] <- passage(vars_tr[[i]],
origin=locations[1],
goal=locations[2], theta = 0.01)
if(length(passages) == nrow(Pj_combn)){
passages_layer <- stack(passages)
passage_layers[[paste(layers[i])]] <- sum(passages_layer)/nrow(Pj_combn)
} else{
print(paste((j/nrow(Pj_combn))*100, "% complete"))
}
}
}
}
plot(states); points(Pj_sp
TN_NC <- states %>%
TNNC <- states %>%
subset(NAMES %>% c("Tennessee", "North Carolina"))
TNNC <- states %>%
subset(NAME %>% c("Tennessee", "North Carolina"))
TNNC <- states %>%
dplyr::subset(NAME %>% c("Tennessee", "North Carolina"))
TNNC <- states %>%
subset(NAME %in% c("Tennessee", "North Carolina"))
TN_NC <- states %>%
subset(NAME %in% c("Tennessee", "North Carolina"))
plot(TN_NC)
crs(TN_NC
)
plot(TN_NC); points(Pj_sp)
plot(Pj_sp); lines(TN_NC)
TN_NC <- states %>% # Subsetting the data to Tennessee and North Carolina
subset(NAME %in% c("Tennessee", "North Carolina"))
Pj_sp <- Pj$gbif$data$Plethodon_jordani %>% # Grabbing the Darwin-core data from the spocc object
dplyr::select(longitude, # Keep locations and year, discard the rest
latitude,
year) %>%
dplyr::filter(year > 2000) %>% # Filter records to only those after year 2000
filter(!duplicated(round(longitude, 2), # Remove duplicate records using rounded decimals (this removes points very near to one-another)
round(latitude, 2)) == TRUE) %>% # >> See notes below about ^^
dplyr::mutate(lon = scale(longitude), # Remove points far outside the cluster of occurrences
lat = scale(latitude)) %>% # >> See notes below about ^^
dplyr::filter(!abs(lon)>2) %>%
dplyr::filter(!abs(lat)>2) %>%
dplyr::select(longitude,
latitude) %>%
SpatialPoints(proj4string = crs(states))
Pj_sp <- Pj$gbif$data$Plethodon_jordani %>% # Grabbing the Darwin-core data from the spocc object
dplyr::select(longitude, # Keep locations and year, discard the rest
latitude,
year) %>%
dplyr::filter(year > 2000) %>% # Filter records to only those after year 2000
filter(!duplicated(round(longitude, 2), # Remove duplicate records using rounded decimals (this removes points very near to one-another)
round(latitude, 2)) == TRUE) %>% # >> See notes below about ^^
dplyr::mutate(lon = scale(longitude), # Remove points far outside the cluster of occurrences
lat = scale(latitude)) %>% # >> See notes below about ^^
dplyr::filter(!abs(lon)>2) %>%
dplyr::filter(!abs(lat)>2) %>%
dplyr::select(longitude,
latitude) %>%
SpatialPoints(proj4string = crs(states))
ggplot(as.data.frame(vars$asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
as.data.frame(vars$asp, xy=T)
ggplot(as.data.frame(vars$asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=asp)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
ggplot(TN_NC) + geom_line(aes(x=longitude, y=latitude))
ggplot(TN_NC) + geom_line(aes(x=lon, y=lat))
ggplot(TN_NC) + geom_line(aes(x=x, y=y))
ggplot(TN_NC) + geom_polygon(aes(x=x, y=y))
ggplot(TN_NC) + geom_polygon(aes(x=longitude, y=latitude))
ggplot(TN_NC) + geom_polygon(aes(x=long, y=lat))
ggplot(TN_NC) + geom_polygon(aes(x=long, y=lat, fill="grey80"))
ggplot(TN_NC) + geom_polygon(aes(x=long, y=lat, fill="grey80")) + coord_quickmap()
ggplot(TN_NC) + geom_polygon(aes(x=long, y=lat, fill="grey80")) + coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(aes(x=long, y=lat, fill=NAME)) +
scale_fill_ coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(aes(x=long, y=lat, fill=NAME)) +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(aes(x=long, y=lat), fill="grey80") +
coord_quickmap() + theme_map()
geom_polygon(data=TN_NC, aes(x=long, y=lat), fill="light blue") +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=states, aes(x=lon, y=lat), fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), fill="light blue") +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=states, aes(x=long, y=lat), fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), fill="light blue") +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=states, aes(x=long, y=lat, group=group), fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), fill="light blue") +
coord_quickmap() + theme_map()
states %>%
filter(!NAME %in% c("Alaska"))
states$GEOID
states$REGION
plot(states, col=states$REGION)
states$REGION
se <- states %>%
subset(REGION == "1")
states %>%
subset(REGION == "1") %>%
plot()
states %>%
subset(REGION == "2") %>%
plot()
states %>%
subset(REGION == "3") %>%
plot()
se <- states %>%
subset(REGION == "3")
TN_NC <- se %>% # Subsetting the data to Tennessee and North Carolina
subset(NAME %in% c("Tennessee", "North Carolina"))
ggplot(TN_NC) + geom_polygon(data=se, aes(x=long, y=lat), fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), fill="light blue") +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=se, aes(x=long, y=lat, grou=group), fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), fill="light blue") +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=se, aes(x=long, y=lat, grou=group), border="grey40", fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), fill="light blue") +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=se, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), fill="light blue") +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=se, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
coord_quickmap() + theme_map()
library(ggplot2)
library(viridis)
library(ggthemes)
ggplot(TN_NC) + geom_polygon(data=se, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
coord_quickmap() + theme_map()
ggsave("se.png", width = 4, height = 3, units = "in", dpi = 200)
ggplot(TN_NC) + geom_polygon(data=se, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=se, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
#geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
coord_quickmap() + theme_map()
Pj_sp@coords
ggplot(TN_NC) + geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
#geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_point(data=Pj_sp@coords, aes(x=latitude, y=longitude)) +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
#geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=latitude, y=longitude)) +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
#geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=latitude, y=longitude)) +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
#geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=latitude, y=longitude))
ggplot(TN_NC) + geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
#geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=longitude, y=latitude)) +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=longitude, y=latitude)) +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=longitude, y=latitude)) +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=se, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=longitude, y=latitude)) +
coord_quickmap() + theme_map()
ggplot(TN_NC) + geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="pink") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=longitude, y=latitude), size=0.01) +
coord_quickmap() + theme_map()
ggplot() + geom_polygon(data=se, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="pink") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=longitude, y=latitude), size=0.01) +
coord_quickmap() + theme_map()
ggplot() + geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="pink") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=longitude, y=latitude), size=0.01) +
coord_quickmap() + theme_map()
?terrain
elv <- elevation %>% # First, lets cut the DEM to our study area
crop(Pj_poly_buff) %>% # crop to the extent
mask(Pj_poly_buff) # mask to the edges
tri <- terrain(elv, opt="TRI", neighbors = 8) # Calculate TRI
set.seed(6)
Pj_sample <- Pj_coords[sample(nrow(Pj_coords), 5),]
ggplot(as.data.frame(tri, xy=T)) + geom_raster(aes(x=x, y=y, fill=tri)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
elv <- elevation %>% # First, lets cut the DEM to our study area
crop(Pj_poly_buff) %>% # crop to the extent
mask(Pj_poly_buff) # mask to the edges
asp <- terrain(elv, opt="aspect", neighbors = 8) # Calculate aspect
tri <- terrain(elv, opt="TRI", neighbors = 8) # Calculate TRI
set.seed(6)
Pj_sample <- Pj_coords[sample(nrow(Pj_coords), 5),]
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=asp)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
Pj_combn <- combn(nrow(Pj_sample),2) %>%
t() %>%
as.matrix()
passages <- list() # Create a list to store the passage probability rasters in
system.time( # Keep track of how long this takes
for (i in 1:nrow(Pj_combn)) {
locations <- SpatialPoints(rbind(Pj_coords[Pj_combn[i,1],1:2], # create origin points
Pj_coords[Pj_combn[i,2],1:2]), # create destination (or goal) points, to traverse
crs(se))
passages[[i]] <- passage(tri_tr, # run the passage function
origin=locations[1], # set orgin point
goal=locations[2], # set goal point
theta = 0.001) # set theta (tuning parameter, see notes below)
print(paste((i/nrow(Pj_combn))*100, "% complete"))
}
)
passages <- stack(passages) # create a raster stack of all the passage probabilities
passages_overlay <- sum(passages)/nrow(Pj_combn) # calculate average
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage", na.value = NA) + theme_map() +
theme(legend.position = "right")
set.seed(6) # To make your results match mine
Pj_sample <- Pj_coords[sample(nrow(Pj_coords), 5),] # Take 5 random locations
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
ggplot(as.data.frame(tri, xy=T)) + geom_raster(aes(x=x, y=y, fill=tri)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
Pj_poly_buff <- gBuffer(Pj_poly, width = 0.05, byid=T)
set.seed(6) # To make your results match mine
Pj_sample <- Pj_coords[sample(nrow(Pj_coords), 5),] # Take 5 random locations
Pj_combn <- combn(nrow(Pj_sample),2) %>%
t() %>%
as.matrix()
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", na.value = NA) + theme_map() +
theme(legend.position = "right")
elv <- elevation %>% # First, lets cut the DEM to our study area
crop(Pj_poly_buff) %>% # crop to the extent
mask(Pj_poly_buff) # mask to the edges
asp <- terrain(elv, opt="aspect", neighbors = 8) # Calculate aspect
tri <- terrain(elv, opt="TRI", neighbors = 8) # Calculate TRI
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
scale_fill_continuous(na.value=NA) + theme_map()
ggplot(as.data.frame(tri, xy=T)) + geom_raster(aes(x=x, y=y, fill=tri)) +
scale_fill_continuous(na.value=NA) + theme_map()
elv <- elevation %>% # First, lets cut the DEM to our study area
crop(Pj_poly_buff) %>% # crop to the extent
mask(Pj_poly_buff) # mask to the edges
asp <- terrain(elv, opt="aspect", neighbors = 8) # Calculate aspect
tri <- terrain(elv, opt="TRI", neighbors = 8) # Calculate TRI
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
scale_fill_continuous(na.value=NA) + theme_map()
ggplot(as.data.frame(tri, xy=T)) + geom_raster(aes(x=x, y=y, fill=tri)) +
scale_fill_continuous(na.value=NA) + theme_map()
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", na.value = NA) + theme_map() +
theme(legend.position = "right")
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", na.value = NA) + theme_map() +
theme(legend.position = "right")
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=log(layer))) +
scale_fill_viridis("Probability of Passage (Flow)", na.value = NA) + theme_map() +
theme(legend.position = "right")
```{r plot flow, fig.height=6, fig.width=9}
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", na.value = NA) + theme_map() +
theme(legend.position = "right")
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", ,"plasma", na.value = NA) + theme_map() +
theme(legend.position = "right")
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", "plasma", na.value = NA) + theme_map() +
theme(legend.position = "right")
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", "magma", na.value = NA) + theme_map() +
theme(legend.position = "right")
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", option="magma", na.value = NA) + theme_map() +
theme(legend.position = "right")
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", option="viridis", na.value = NA) + theme_map() +
theme(legend.position = "right")
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", option="plasma", na.value = NA) + theme_map() +
theme(legend.position = "right")
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", option="inferno", na.value = NA) + theme_map() +
theme(legend.position = "right")
passages <- list() # Create a list to store the passage probability rasters in
system.time( # Keep track of how long this takes
for (i in 1:nrow(Pj_combn)) {
locations <- SpatialPoints(rbind(Pj_coords[Pj_combn[i,1],1:2], # create origin points
Pj_coords[Pj_combn[i,2],1:2]), # create destination (or goal) points, to traverse
crs(se))
passages[[i]] <- passage(tri_tr, # run the passage function
origin=locations[1], # set orgin point
goal=locations[2], # set goal point
theta = 0.01) # set theta (tuning parameter, see notes below)
print(paste((i/nrow(Pj_combn))*100, "% complete"))
}
)
passages <- stack(passages) # create a raster stack of all the passage probabilities
passages_overlay <- sum(passages)/nrow(Pj_combn) # calculate average
ggplot(as.data.frame(passages_overlay, xy=T)) + geom_raster(aes(x=x,y=y,fill=layer)) +
scale_fill_viridis("Probability of Passage (Flow)", option="inferno", na.value = NA) + theme_map() +
theme(legend.position = "right")
library(tigris) # Tigris package for USA census data
library(tidyverse)
states <- states()
se <- states %>%
subset(REGION == "3")
TN_NC <- se %>% # Subsetting the data to Tennessee and North Carolina
subset(NAME %in% c("Tennessee", "North Carolina"))
blogdown::new_post("test", ext=".md")
knit_with_parameters('C:/Users/jbaecher/Dropbox (UFL)/website/content/post/connectivity_script.Rmd', encoding = 'UTF-8')
library(tigris) # Tigris package for USA census data
library(tidyverse)
states <- states()
se <- states %>%
subset(REGION == "3")
TN_NC <- se %>% # Subsetting the data to Tennessee and North Carolina
subset(NAME %in% c("Tennessee", "North Carolina"))
library(spocc)
library(raster)
Pj <- occ(query = "Plethodon jordani", # JRCS scientific name
from = "gbif", # limiting query to *the first* 1000 records
limit=1000, # limiting query to *the first* 1000 records
has_coords = T) # limiting those 1000 records to those that have geo-referenced data
blogdown::new_post("connectivity",ext=".md")
####### Set circuitscape exe file location #######
CS_exe <- 'C:/"Program Files"/Circuitscape/cs_run.exe' # Don't forget the "Program Files" problem
library(tigris) # Tigris package for USA census data
library(tidyverse)
states <- states()
se <- states %>%
subset(REGION == "3")
TN_NC <- se %>% # Subsetting the data to Tennessee and North Carolina
subset(NAME %in% c("Tennessee", "North Carolina"))
library(tigris) # Tigris package for USA census data
library(tidyverse)
states <- states()
se <- states %>%
subset(REGION == "3")
TN_NC <- se %>% # Subsetting the data to Tennessee and North Carolina
subset(NAME %in% c("Tennessee", "North Carolina"))
library(spocc)
library(raster)
Pj <- occ(query = "Plethodon jordani", # JRCS scientific name
from = "gbif", # limiting query to *the first* 1000 records
limit=1000, # limiting query to *the first* 1000 records
has_coords = T) # limiting those 1000 records to those that have geo-referenced data
Pj_sp <- Pj$gbif$data$Plethodon_jordani %>% # Grabbing the Darwin-core data from the spocc object
dplyr::select(longitude, # Keep locations and year, discard the rest
latitude,
year) %>%
dplyr::filter(year > 2000) %>% # Filter records to only those after year 2000
filter(!duplicated(round(longitude, 2), # Remove duplicate records using rounded decimals (this removes points very near to one-another)
round(latitude, 2)) == TRUE) %>% # >> See notes below about ^^
dplyr::mutate(lon = scale(longitude), # Remove points far outside the cluster of occurrences
lat = scale(latitude)) %>% # >> See notes below about ^^
dplyr::filter(!abs(lon)>2) %>%
dplyr::filter(!abs(lat)>2) %>%
dplyr::select(longitude,
latitude) %>%
SpatialPoints(proj4string = crs(se))
library(ggplot2)
library(viridis)
library(ggthemes)
ggplot() + geom_polygon(data=se, aes(x=long, y=lat, grou=group), col="grey40", fill="grey80") +
geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
coord_quickmap() + theme_map()
Pj_coords <- Pj_sp@coords
Pj_chull <- chull(Pj_sp@coords) # Creating convex hull
Pj_chull_ends <- Pj_sp@coords[c(Pj_chull, Pj_chull[1]),] # generate the end points of polygon.
Pj_poly <- SpatialPolygons(
list(Polygons(
list(Polygon(Pj_chull_ends)), ID=1)),
proj4string = crs(se)) # convert coords to SpatialPolygons
library(rgeos)
Pj_poly_buff <- gBuffer(Pj_poly, width = 0.05, byid=T)
ggplot() + geom_polygon(data=TN_NC, aes(x=long, y=lat), col="grey40", fill="light blue") +
geom_polygon(data=Pj_poly_buff, aes(x=long, y=lat, grou=group), col="grey40", fill="pink") +
geom_point(data=as.data.frame(Pj_sp@coords), aes(x=longitude, y=latitude), size=0.01) +
coord_quickmap() + theme_map()
library(elevatr)
elevation <- get_elev_raster(Pj_poly_buff, z = 8) # This will find a DEM tile nearest to our polygon
elv <- elevation %>% # First, lets cut the DEM to our study area
crop(Pj_poly_buff) %>% # crop to the extent
mask(Pj_poly_buff) # mask to the edges
asp <- terrain(elv, opt="aspect", neighbors = 8) # Calculate aspect
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
scale_fill_continuous(na.value=NA) + theme_map() + theme(legend.position = "right")
set.seed(6) # To make your results match mine
Pj_sample <- Pj_coords[sample(nrow(Pj_coords), 5),] # Take 5 random locations
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
sites <- rasterize(x = Pj_sample, y = asp)
####### Write rasters #######
writeRaster(sites,"sites_rast.asc", overwrite=TRUE)
plot(sites)
####### Write rasters #######
writeRaster(sites, "sites_rast.asc")
sites
asp
Pj_sample
asp
elevation
crs(asp) == crs(Pj_sample)
crs(asp)
crs(Pj_sample)
set.seed(6) # To make your results match mine
Pj_sample <- Pj_coords[sample(nrow(Pj_coords), 5),] # Take 5 random locations
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
Pj_coords
Pj_sp
Pj_sample <- Pj_coords[sample(nrow(Pj_coords), 5),] %>% # Take 5 random locations
SpatialPoints(crs(se))
Pj_sample
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
set.seed(6) # To make your results match mine
Pj_sample <- Pj_coords[sample(nrow(Pj_coords), 5),] %>% # Take 5 random locations
SpatialPoints(crs(se))
set.seed(6) # To make your results match mine
Pj_sample <- Pj_coords[sample(nrow(Pj_coords), 5),] %>% # Take 5 random locations
SpatialPoints(crs(se))
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
ggplot(as.data.frame(asp, xy=T)) + geom_raster(aes(x=x, y=y, fill=aspect)) +
geom_point(data=as.data.frame(Pj_sample), aes(x=longitude, y=latitude), size=2, col="white") +
scale_fill_continuous(na.value=NA) + theme_map()
crs(asp) == crs(Pj_sample)
crs(Pj_sample)
crs(asp)
crs(asp)
crs(Pj_sample)
asp
blogdown::new_content("content/talk",".md")