-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01_spatial-thinning.Rmd
47 lines (36 loc) · 1.22 KB
/
01_spatial-thinning.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
---
editor_options:
chunk_output_type: console
---
# Spatial thinning of occurrence data
In this script, we spatially thin occurrence data for creating Figure 1.
## Load necessary libraries
```{r}
library(sf)
library(dplyr)
library(spThin)
```
## Load data
```{r}
loc <- read.csv("data/localities-for-map.csv")
head(loc)
loc_unique <- loc %>% distinct()
loc_unique$species <- "species"
# carry out spatial thinning with a minimum distance of 15km between records
thinned <- thin(loc_unique,lat.col="LATITUDE",long.col = "LONGITUDE",
spec.col = "species",thin.par = 15,reps=1,
write.files=T,
out.dir="data/",
out.base="localities-thinned")
# reload spatially thinned file
thin_file <- read.csv("results/localities-for-map-thinned.csv")
thin_file <- thin_file %>%
mutate(region = case_when(LONGITUDE < 83 ~ "west",
LONGITUDE > 83 ~ "east"))
shp <- st_as_sf(thin_file, coords = c("LONGITUDE","LATITUDE"), crs=4326)
str(shp)
st_write(shp[shp$region=="east",],"data/shapefiles/localities-east.shp",
driver="ESRI Shapefile")
st_write(shp[shp$region=="west",],"data/shapefiles/localities-west.shp",
driver="ESRI Shapefile")
```