# Introduction to rgbif

#### Scott Chamberlain

#### 2020-03-09

Seach and retrieve data from the Global Biodiverity Information Facilty (GBIF)

## About the package

`rgbif` is an R package to search and retrieve data from the Global Biodiverity Information Facilty (GBIF). `rgbif` wraps R code around the GBIF API to allow you to talk to GBIF from R.

## Get rgbif

Install from CRAN

    install.packages("rgbif")

Or install the development version from GitHub

    devtools::install_github("ropensci/rgbif")

Load rgbif

    library("rgbif")

## Number of occurrences

Search by type of record, all observational in this case

    occ_count(basisOfRecord='OBSERVATION')

Records for Puma concolor with lat/long data (georeferened) only. Note that `hasCoordinate` in `occ_search()` is the same as `georeferenced` in `occ_count()`.

    occ_count(taxonKey=2435099, georeferenced=TRUE)

All georeferenced records in GBIF

    occ_count(georeferenced=TRUE)

Records from Denmark

    denmark_code <- isocodes[grep("Denmark", isocodes$name), "code"]
    occ_count(country=denmark_code)

Number of records in a particular dataset

    occ_count(datasetKey='9e7ea106-0bf8-4087-bb61-dfe4f29e0f17')

All records from 2012

    occ_count(year=2012)

Records for a particular dataset, and only for preserved specimens

    occ_count(datasetKey='e707e6da-e143-445d-b41d-529c4a777e8b', basisOfRecord='OBSERVATION')

## Search for taxon names

Get possible values to be used in taxonomic rank arguments in functions

    taxrank()

`name_lookup()` does full text search of name usages covering the scientific and vernacular name, the species description, distribution and the entire classification across all name usages of all or some checklists. Results are ordered by relevance as this search usually returns a lot of results.

By default `name_lookup()` returns five slots of information: meta, data, facets, hierarchies, and names. hierarchies and names elements are named by their matching GBIF key in the `data.frame` in the data slot.

    out <- name_lookup(query='mammalia')

    names(out)

    out$meta

    head(out$data)

    out$facets

    out$hierarchies[1:2]

    out$names[2]

Search for a genus

    head(name_lookup(query='Cnaemidophorus', rank="genus", return="data"))

Search for the class mammalia

    head(name_lookup(query='mammalia', return = 'data'))

Look up the species Helianthus annuus

    head(name_lookup(query = 'Helianthus annuus', rank="species", return = 'data'))

The function `name_usage()` works with lots of different name endpoints in GBIF, listed at http://www.gbif.org/developer/species#nameUsages.

    name_usage(key=3119195, language="FRENCH", data='vernacularNames')

The function `name_backbone()` is used to search against the GBIF backbone taxonomy

    name_backbone(name='Helianthus', rank='genus', kingdom='plants')

The function `name_suggest()` is optimized for speed, and gives back suggested names based on query parameters.

    head( name_suggest(q='Puma concolor') )

## Single occurrence records

Get data for a single occurrence. Note that data is returned as a list, with slots for metadata and data, or as a hierarchy, or just data.

Just data

    occ_get(key=855998194, return='data')

Just taxonomic hierarchy

    occ_get(key=855998194, return='hier')

All data, or leave return parameter blank

    occ_get(key=855998194, return='all')

Get many occurrences. `occ_get` is vectorized

    occ_get(key=c(855998194, 240713150), return='data')

## Search for occurrences

By default `occ_search()` returns a `dplyr` like output summary in which the data printed expands based on how much data is returned, and the size of your window. You can search by scientific name:

    occ_search(scientificName = "Ursus americanus", limit = 20)

Or to be more precise, you can search for names first, make sure you have the right name, then pass the GBIF key to the `occ_search()` function:

    key <- name_suggest(q='Helianthus annuus', rank='species')$key[1]
    occ_search(taxonKey=key, limit=20)

Like many functions in `rgbif`, you can choose what to return with the `return` parameter, here, just returning the metadata:

    occ_search(taxonKey=key, return='meta')

You can choose what fields to return. This isn’t passed on to the API query to GBIF as they don’t allow that, but we filter out the columns before we give the data back to you.

    occ_search(scientificName = "Ursus americanus", fields=c('name','basisOfRecord','protocol'), limit = 20)

Most parameters are vectorized, so you can pass in more than one value:

    splist <- c('Cyanocitta stelleri', 'Junco hyemalis', 'Aix sponsa')
    keys <- sapply(splist, function(x) name_suggest(x)$key[1], USE.NAMES=FALSE)
    occ_search(taxonKey=keys, limit=5)

## Maps

Using thet GBIF map web tile service, making a raster and visualizing it.

    x <- map_fetch(taxonKey = 2480498, year = 2000:2017)
    library(raster)
    plot(x)