Downloading species occurrence data from GBIF using R.

In this blog post, we will be discussing how to use R to access and manipulate biodiversity data using the GBIF (Global Biodiversity Information Facility) API. We will be using three libraries: rgbif, sf, and spocc.

  • The rgbif library provides an R interface for the GBIF API, allowing us to access and manipulate biodiversity data available on GBIF, as well as download and modify it as needed.
  • The sf library provides a simple, user-friendly way to manipulate spatial vector data in R.
  • Finally, the spocc library is a specialized interface for the GBIF API that is specifically designed for ecology and biogeography. It complements rgbif by providing a format specific to ecology and biogeography.

Here are the steps we will be following:

  1. Load the rgbif, sf, and spocc libraries.

library(rgbif)
library(sf)
library(spocc)

2. Set the working directory.

setwd("D:/RUTA")

3. Define the name of the species we will be searching for on GBIF.

name_sp <- "name of the species"

4. Load a shapefile that defines the area we will be searching within (e.g. a country, province, or canton).

countries <- st_read("countries.shp")

5. Use the “occ" function to query GBIF using the species name and the geometry limits of the countries contained within the shapefile. The function can also be configured to return only observations with geographic coordinates, and to limit the total number of observations returned to 1000.

df_occ1 <- occ(query = name_sp, from = "gbif", geometry = st_bbox(countries),
has_coords = TRUE, limit = 1000)$gbif$data[[1]]

6. Use lapply to convert all columns to characters.

df_occ2 <- as.data.frame(lapply(df_occ1, as.character), stringsAsFactors=T)

7. Use the write.csv function to save the dataframe “df_occ2" to a CSV file.

write.csv(df_occ2, paste0("nombre del archivo.csv"),
row.names = F)

Made with ChatGPT

Leave a Comment