Skip to content

Commit 783fd8a

Browse files
final tweaks for publication
1 parent e343685 commit 783fd8a

27 files changed

+23150
-11646
lines changed

correlative_model/01_spatial_thin.R

+48-48
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
1-
##############################
2-
###### Spatial thinning ######
3-
##############################
4-
5-
6-
# Load libraries
7-
library(spThin)
8-
library(ggplot2)
9-
library(rnaturalearth)
10-
11-
# Set your paths
12-
source("paths.R")
13-
14-
#read in the data
15-
data <- read.csv(path2allpresences, header=T)
16-
17-
#plot the species presence points
18-
plot(data$x, data$y)
19-
data$Species<-"target_group"
20-
#thin the data
21-
thinned_data_full <- thin(loc.data=data, long.col = "x", lat.col="y", spec.col="Species",
22-
thin.par=5, reps=100, # thin.par specifies the minimum distance between points in kilometers
23-
locs.thinned.list.return = TRUE,
24-
write.files=FALSE,write.log.file=FALSE)
25-
26-
#All datasets seem to retain the same number of occurences. Plot one of them above the previous plot to show which points are retained.
27-
thinned<-thinned_data_full[[1]]
28-
points(thinned$Longitude, thinned$Latitude, col="red")# plots the retained points over the plot previously made
29-
30-
names(thinned) <- c("x", "y")
31-
32-
# now retrieve all the columns from the data dataframe and merge them with the columns from the thinned dataframe
33-
spt<-merge(thinned,data,by= c("row.names", "x", "y"))
34-
35-
#Plot the datapoints on a map:
36-
sf_world <- ne_countries(returnclass = "sf")
37-
tibble::glimpse(sf_world)
38-
ggplot(sf_world) +
39-
geom_sf()+
40-
geom_point(data=thinned, aes(x=x, y=y))
41-
42-
#export the dataframe with the selected points
43-
write.csv(thinned,file=path2presence,row.names=FALSE)
44-
45-
#Transform this data into a shapefile for easy visualisation in gis software
46-
# WGScoor <- spt
47-
# coordinates(WGScoor)=~x+y
48-
# proj4string(WGScoor)<-CRS("+proj=longlat +datum=WGS84")
1+
##############################
2+
###### Spatial thinning ######
3+
##############################
4+
5+
6+
# Load libraries
7+
library(spThin)
8+
library(ggplot2)
9+
library(rnaturalearth)
10+
11+
# Set your paths
12+
source("paths.R")
13+
14+
#read in the data
15+
data <- read.csv(path2allpresences, header=T)
16+
17+
#plot the species presence points
18+
plot(data$x, data$y)
19+
data$Species<-"target_group"
20+
#thin the data
21+
thinned_data_full <- thin(loc.data=data, long.col = "x", lat.col="y", spec.col="Species",
22+
thin.par=5, reps=100, # thin.par specifies the minimum distance between points in kilometers
23+
locs.thinned.list.return = TRUE,
24+
write.files=FALSE,write.log.file=FALSE)
25+
26+
#All datasets seem to retain the same number of occurences. Plot one of them above the previous plot to show which points are retained.
27+
thinned<-thinned_data_full[[1]]
28+
points(thinned$Longitude, thinned$Latitude, col="red")# plots the retained points over the plot previously made
29+
30+
names(thinned) <- c("x", "y")
31+
32+
# now retrieve all the columns from the data dataframe and merge them with the columns from the thinned dataframe
33+
spt<-merge(thinned,data,by= c("row.names", "x", "y"))
34+
35+
#Plot the datapoints on a map:
36+
sf_world <- ne_countries(returnclass = "sf")
37+
tibble::glimpse(sf_world)
38+
ggplot(sf_world) +
39+
geom_sf()+
40+
geom_point(data=thinned, aes(x=x, y=y))
41+
42+
#export the dataframe with the selected points
43+
write.csv(thinned,file=path2presence,row.names=FALSE)
44+
45+
#Transform this data into a shapefile for easy visualisation in gis software
46+
# WGScoor <- spt
47+
# coordinates(WGScoor)=~x+y
48+
# proj4string(WGScoor)<-CRS("+proj=longlat +datum=WGS84")
4949
# raster::shapefile(WGScoor,"thinned_data.shp",overwrite=TRUE)

0 commit comments

Comments
 (0)