We can download the amphibian data we extracted from the DASCO v2.5 data (after adding taxonomic information). We also download the geographic data of world mountains. The following code downloads these files to a local folder defined by the user:
Now unzip the mountain polygons file into the same local folder.
1.1 Load data
The following code loads the mountain polygons (selecting specific columns) and the DASCO v2.5 amphibian records:
Code
# Load amphibians DASCO v2.5dasco_amphibians <-fread(file.path(local_folder, "DASCO_v2.5_Amphibians.csv"))# Load polygons delimitating mountains (Standard basic layer from Snethlage et al. 2022)mountain_polygons <-vect(file.path(local_folder , "GMBA_Inventory_v2.0_standard_basic.shp"))# select specific columnsmountain_polygons <- mountain_polygons[, c("MapName","GMBA_V2_ID","MapUnit","Area","Perimeter","Elev_Low","Elev_High","Level_01","Level_02","Level_03","Level_04","Level_05","Level_06","Level_07","Level_08","Level_09","Level_10","Countries","Feature")]
2 Extracting records in mountains
2.1 Spatial Join with Mountain Polygons
This section performs a spatial join to extract polygon data for each point, identifying which occurrence records fall within mountain boundaries.
Code
# Convert points and mountain polygons to sf objectsdasco_sf <-st_as_sf(x = dasco_amphibians,coords =c("Longitude", "Latitude"),crs =4326)mountains_sf <-st_as_sf(mountain_polygons)mountains_sf <-st_make_valid(mountains_sf) # make sure geometries are validmountains_sf <-st_simplify(mountains_sf, dTolerance =0.001) # Simplify complex geometric relationships# Perform a spatial join to extract polygon data for each point# Note: Use st_join() to join points and polygons based on their spatial relationshipdasco_mountains <-st_join(dasco_sf, mountains_sf, join = st_intersects)head(dasco_mountains)
taxon
location
Realm
Database
speciesKey
kingdom
phylum
class
order
family
MapName
GMBA_V2_ID
MapUnit
Area
Perimeter
Elev_Low
Elev_High
Level_01
Level_02
Level_03
Level_04
Level_05
Level_06
Level_07
Level_08
Level_09
Level_10
Countries
Feature
geometry
Afrixalus fulvovittatus
Ghana
terrestrial
GBIF
2426552
Animalia
Chordata
Amphibia
Anura
Hyperoliidae
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
POINT (-0.217 5.55)
Afrixalus fulvovittatus
Ghana
terrestrial
GBIF
2426552
Animalia
Chordata
Amphibia
Anura
Hyperoliidae
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
POINT (-2.489 10.07917)
Afrixalus fulvovittatus
Ghana
terrestrial
GBIF
2426552
Animalia
Chordata
Amphibia
Anura
Hyperoliidae
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
POINT (-2.509667 10.0585)
Afrixalus fulvovittatus
Ghana
terrestrial
GBIF
2426552
Animalia
Chordata
Amphibia
Anura
Hyperoliidae
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
POINT (-1.006333 9.491167)
Afrixalus fulvovittatus
Ghana
terrestrial
GBIF
2426552
Animalia
Chordata
Amphibia
Anura
Hyperoliidae
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
POINT (-0.984333 9.397833)
Afrixalus fulvovittatus
Ghana
terrestrial
GBIF
2426552
Animalia
Chordata
Amphibia
Anura
Hyperoliidae
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
NA
POINT (-2.6455 5.26405)
Code
# Remove records not overlapping with mountainsamphibians_in_mountains <- dasco_mountains %>%drop_na(MapName)
We can also save the extracted records in mountains in a data frame format:
Code
# Convert geometry to separate lon/lat columnsamphibians_in_mountains_df <- amphibians_in_mountains %>%mutate(lon =st_coordinates(.)[,1],lat =st_coordinates(.)[,2] ) %>%st_drop_geometry() # Remove geometry column
3 Extracting mountain with records
Here we subset the mountain polygons to keep only those mountains with amphibian records:
Code
# Subset the mountain polygon to keep only the mountains with recordsmountains_with_records <-unique(amphibians_in_mountains$MapName)mountains_with_records_shp <- mountain_polygons[mountain_polygons$MapName %in% mountains_with_records]
4 Plot data
We can visualize the results by plotting the mountain polygons with records and overlaying the amphibian occurrence points. Let’s first create a world map higlighting the selected mountain ranges:
Code
# Plot world mapmaps::map("world", col ="#A0DFB980", fill =TRUE, border ="white",xlim =c(-180, 180), ylim =c(-60, 80))# Add mountain polygons on topplot(mountains_with_records_shp, add =TRUE, col ="#366A9F80", border ="#366A9F80", lwd =1)
Now we can add the amphibian occurrence points on top of the mountain polygons:
Finally we can save the results as shapefiles and a CSV file for further analysis:
Code
# Save as .shpst_write( amphibians_in_mountains,file.path(local_folder, "amphibians_in_mountains.shp"),append =FALSE)# Saving in the Shiny folder as this will be used for visualizationwriteVector( mountains_with_records_shp,file.path(local_folder, "/Shp_Mt_W_records.shp"),filetype ="ESRI Shapefile")# Write to CSVdata.table::fwrite( amphibians_in_mountains_df,file.path(local_folder, "amphibians_in_mountains.csv"))
---title: Extract mountain observations from DASCO datasubtitle: RAVeMauthor: Adrian García-Rodríguez, David Umhauer & Marcelo Araya-Salasdate: "`r Sys.Date()`"toc: truetoc-depth: 3toc-location: leftnumber-sections: truehighlight-style: pygmentsformat: html: df-print: kable code-fold: show code-tools: true css: qmd.csseditor_options: chunk_output_type: console---```{r set root directory}#| eval: true#| echo: false# install knitr package if not installedif (!requireNamespace("knitr", quietly =TRUE)) {install.packages("knitr")}# set working directory knitr::opts_knit$set(root.dir ="..")``````{r setup style}#| message: false#| warning: false#| echo: false# options to customize chunk outputsknitr::opts_chunk$set(# tidy.opts = list(width.cutoff = 65), # tidy = TRUE,message =FALSE )local_folder <-"./data/processed"```<!-- skyblue box -->::: {.alert .alert-info}# Purpose {.unnumbered .unlisted}- Extract observations from the DASCO data set that occur in mountains on the example amphibian data:::# Load package {.unnumbered .unlisted}```{r load packages}#| eval: true# install packages if not installedif (!requireNamespace("sketchy", quietly =TRUE)) {install.packages("sketchy")}packages <-c("plyr", "dplyr", "tidyverse", "sf", "terra", "data.table")# install/ load packagessketchy::load_packages(packages = packages)```# Download/read dataWe can download the amphibian data we extracted from the DASCO v2.5 data (after adding taxonomic information). We also download the geographic data of world mountains. The following code downloads these files to a local folder defined by the user:```{r}#| eval: false# download observations download.file("https://figshare.com/ndownloader/files/58775401",destfile =file.path(local_folder, "DASCO_v2.5_Amphibians.csv"),mode ="wb")# download mountain polygonsdownload.file("https://figshare.com/ndownloader/files/58776649",destfile =file.path(local_folder, "GMBA_Inventory_v2.0_standard_basic.zip"),mode ="wb")```The files can also be downloaded manually from these links: - [amphibian observations](https://figshare.com/ndownloader/files/58775401)- [mountain polygons](https://figshare.com/ndownloader/files/58776457)Now unzip the mountain polygons file into the same local folder.## Load dataThe following code loads the mountain polygons (selecting specific columns) and the DASCO v2.5 amphibian records:```{r load-data}# Load amphibians DASCO v2.5dasco_amphibians <-fread(file.path(local_folder, "DASCO_v2.5_Amphibians.csv"))# Load polygons delimitating mountains (Standard basic layer from Snethlage et al. 2022)mountain_polygons <-vect(file.path(local_folder , "GMBA_Inventory_v2.0_standard_basic.shp"))# select specific columnsmountain_polygons <- mountain_polygons[, c("MapName","GMBA_V2_ID","MapUnit","Area","Perimeter","Elev_Low","Elev_High","Level_01","Level_02","Level_03","Level_04","Level_05","Level_06","Level_07","Level_08","Level_09","Level_10","Countries","Feature")]```# Extracting records in mountains## Spatial Join with Mountain PolygonsThis section performs a spatial join to extract polygon data for each point, identifying which occurrence records fall within mountain boundaries.```{r spatial-join-prep}# Convert points and mountain polygons to sf objectsdasco_sf <-st_as_sf(x = dasco_amphibians,coords =c("Longitude", "Latitude"),crs =4326)mountains_sf <-st_as_sf(mountain_polygons)mountains_sf <-st_make_valid(mountains_sf) # make sure geometries are validmountains_sf <-st_simplify(mountains_sf, dTolerance =0.001) # Simplify complex geometric relationships# Perform a spatial join to extract polygon data for each point# Note: Use st_join() to join points and polygons based on their spatial relationshipdasco_mountains <-st_join(dasco_sf, mountains_sf, join = st_intersects)head(dasco_mountains)# Remove records not overlapping with mountainsamphibians_in_mountains <- dasco_mountains %>%drop_na(MapName)```We can also save the extracted records in mountains in a data frame format:```{r}# Convert geometry to separate lon/lat columnsamphibians_in_mountains_df <- amphibians_in_mountains %>%mutate(lon =st_coordinates(.)[,1],lat =st_coordinates(.)[,2] ) %>%st_drop_geometry() # Remove geometry column```# Extracting mountain with recordsHere we subset the mountain polygons to keep only those mountains with amphibian records:```{r subset-mountains}# Subset the mountain polygon to keep only the mountains with recordsmountains_with_records <-unique(amphibians_in_mountains$MapName)mountains_with_records_shp <- mountain_polygons[mountain_polygons$MapName %in% mountains_with_records]```# Plot dataWe can visualize the results by plotting the mountain polygons with records and overlaying the amphibian occurrence points. Let's first create a world map higlighting the selected mountain ranges:```{r}# Plot world mapmaps::map("world", col ="#A0DFB980", fill =TRUE, border ="white",xlim =c(-180, 180), ylim =c(-60, 80))# Add mountain polygons on topplot(mountains_with_records_shp, add =TRUE, col ="#366A9F80", border ="#366A9F80", lwd =1)```Now we can add the amphibian occurrence points on top of the mountain polygons:```{r}#| eval: falsepoints( amphibians_in_mountains$geometry,col =adjustcolor("green4", alpha.f =0.1),pch =20,cex =0.1)``````{r}#| echo: false# Plot world mapmaps::map("world", col ="#A0DFB980", fill =TRUE, border ="white",xlim =c(-180, 180), ylim =c(-60, 80))# Add mountain polygons on topplot(mountains_with_records_shp, add =TRUE, col ="#366A9F80", border ="#366A9F80", lwd =1)points( amphibians_in_mountains$geometry,col =adjustcolor("tomato", alpha.f =0.1),pch =20,cex =0.1)```# Save ResultsFinally we can save the results as shapefiles and a CSV file for further analysis:```{r save-shapefile}#| eval: false# Save as .shpst_write( amphibians_in_mountains,file.path(local_folder, "amphibians_in_mountains.shp"),append =FALSE)# Saving in the Shiny folder as this will be used for visualizationwriteVector( mountains_with_records_shp,file.path(local_folder, "/Shp_Mt_W_records.shp"),filetype ="ESRI Shapefile")# Write to CSVdata.table::fwrite( amphibians_in_mountains_df,file.path(local_folder, "amphibians_in_mountains.csv"))```<!-- add packages used, system details and versions --># Session information {.unnumbered .unlisted}<details><summary>Click to see</summary>```{r session info}#| echo: false# if devtools is installed use devtools::session_info()if (requireNamespace("devtools", quietly =TRUE)) { devtools::session_info()} else {sessionInfo()}```</details>