Install Packages

call terra and rspat package

library(terra)     # Spatial data analysis
library(rspat)     # Spatial distribution modeling
bf <- spat_data("bigfoot")  # Load dataset for analysis

Observation

dim(bf)   # Get data dimensions
## [1] 3092    3
head(bf)  # Preview first few rows of data
##         lon      lat Class
## 1 -142.9000 61.50000     A
## 2 -132.7982 55.18720     A
## 3 -132.8202 55.20350     A
## 4 -141.5667 62.93750     A
## 5 -149.7853 61.05950     A
## 6 -141.3165 62.77335     A

Plot: Species Occurrence Points

plot(bf[,1:2], cex=0.5, col="red")  # Plot species occurrence points
library(geodata)                    # Load geodata
wrld <- geodata::world(path=".")    # Load world boundaries
bnds <- wrld[wrld$NAME_0 %in% 
               c("Canada", "Mexico", "United States"), ] 
lines(bnds)                         # Overlay boundaries

This image shows a map of North America with red dots representing the occurrence points of a specific species. The red dots are mainly concentrated in the United States and Mexico, illustrating the species’ geographical distribution.

Visualization: Climate Data

wc <- geodata::worldclim_global("bio", res=10, ".")   # Load climate data
plot(wc[[c(1, 12)]], nr=2)                           # Plot temperature and precipitation

– Map 1 : This map shows global temperature distribution, with colors ranging from blue (cold) to yellow (warm).

– Map 2 : This map illustrates global precipitation levels, with colors ranging from purple (low precipitation) to green (high precipitation).

Climate Data

bfc <- extract(wc, bf[,1:2])  # Extract climate data for species occurrence points
head(bfc, 3)                  # Preview the first 3 rows
##   ID wc2.1_10m_bio_1 wc2.1_10m_bio_2 wc2.1_10m_bio_3 wc2.1_10m_bio_4
## 1  1       -1.832979       12.504708        28.95899       1152.4308
## 2  2        6.360650        5.865935        32.27475        462.5731
## 3  3        6.360650        5.865935        32.27475        462.5731
##   wc2.1_10m_bio_5 wc2.1_10m_bio_6 wc2.1_10m_bio_7 wc2.1_10m_bio_8
## 1        20.34075      -22.840000        43.18075        5.327750
## 2        16.65505       -1.519947        18.17500        3.964495
## 3        16.65505       -1.519947        18.17500        3.964495
##   wc2.1_10m_bio_9 wc2.1_10m_bio_10 wc2.1_10m_bio_11 wc2.1_10m_bio_12
## 1      -0.6887083         11.80792       -16.038542              991
## 2      10.4428196         12.28183         1.467686             3079
## 3      10.4428196         12.28183         1.467686             3079
##   wc2.1_10m_bio_13 wc2.1_10m_bio_14 wc2.1_10m_bio_15 wc2.1_10m_bio_16
## 1              120               42         31.32536              337
## 2              448              141         35.27518             1127
## 3              448              141         35.27518             1127
##   wc2.1_10m_bio_17 wc2.1_10m_bio_18 wc2.1_10m_bio_19
## 1              157              288              216
## 2              468              630              873
## 3              468              630              873
  • Extract climate data (e.g., temperature, precipitation) at the locations of species occurrences.

Remove the First Column with the ID

bfc <- bfc[,-1]  # Remove the ID column

Plot the Species’ Distribution

plot(bfc[ ,"wc2.1_10m_bio_1"], bfc[, "wc2.1_10m_bio_12"], col="red", xlab="Annual mean temperature (.C)", ylab="Annual precipitation (mm)") 

  • Purpose: Visualize the relationship between two key climate variables.
  • Interpretation: A scatter plot of annual mean temperature vs. precipitation for species occurrences.
  • Use: Explore the climate preferences or niches of the species.

Background data

ext_bf <- ext(vect(bf[, 1:2])) + 1  # Create an extended spatial extent around occurrence points
ext_bf                             # Display the extent 
## SpatExtent : -157.75, -63.4627, 24.141, 70.5 (xmin, xmax, ymin, ymax)

Take 5000 Random Samples

set.seed(0) 
window(wc) <- ext_bf 
bg <- spatSample(wc, 5000, "random", na.rm=TRUE, xy=TRUE) 
head(bg) 
##           x        y wc2.1_10m_bio_1 wc2.1_10m_bio_2 wc2.1_10m_bio_3
## 1  -99.2500 66.75000     -13.2934895        7.870646        14.96619
## 2 -106.0833 42.08333       5.6722708       14.530958        36.82943
## 3 -111.9167 46.58333       6.7605939       14.135854        35.23372
## 4 -106.9167 54.75000       0.4086979       11.528605        24.43290
## 5 -118.2500 67.08333      -9.1363859        8.185354        16.34505
## 6 -111.2500 38.91667       8.4194584       15.997125        38.84047
##   wc2.1_10m_bio_4 wc2.1_10m_bio_5 wc2.1_10m_bio_6 wc2.1_10m_bio_7
## 1       1638.6833        15.42850       -37.16100        52.58950
## 2        894.3715        27.86600       -11.58875        39.45475
## 3        927.7927        28.14375       -11.97650        40.12025
## 4       1290.1088        22.55225       -24.63250        47.18475
## 5       1567.0846        17.46575       -32.61275        50.07850
## 6        904.0610        30.49050       -10.69625        41.18675
##   wc2.1_10m_bio_8 wc2.1_10m_bio_9 wc2.1_10m_bio_10 wc2.1_10m_bio_11
## 1        6.484917      -31.617332         7.518209        -31.76942
## 2        9.226916       -4.839750        17.168291         -4.83975
## 3       15.638333       -4.921750        18.186209         -4.92175
## 4       15.417084      -13.864500        15.417084        -16.31392
## 5        8.609292      -21.353209        10.573625        -27.49783
## 6       19.076958        3.179209        19.812834         -2.50475
##   wc2.1_10m_bio_12 wc2.1_10m_bio_13 wc2.1_10m_bio_14 wc2.1_10m_bio_15
## 1              171               33                4         70.29919
## 2              288               42               13         38.78144
## 3              293               48                9         53.40759
## 4              471               86               16         58.32499
## 5              223               43                7         61.21693
## 6              228               28               11         32.40370
##   wc2.1_10m_bio_16 wc2.1_10m_bio_17 wc2.1_10m_bio_18 wc2.1_10m_bio_19
## 1               90               13               78               13
## 2              112               41               90               41
## 3              129               35              115               35
## 4              220               53              220               56
## 5              108               27               93               29
## 6               83               40               72               44
  • Purpose: Generate random background points from the climate data within the extent.
  • Use: Serves as control data for comparison with species occurrences in modeling.

Plot spatSample

plot(bg[, c("x", "y")]) 

  • Purpose: Visualize the distribution of randomly sampled points.
  • Interpretation: A scatter plot showing the locations of sampled points.
  • Use: Confirms spatial coverage and distribution of background data.