1. Download records of Magnoliopsida and Liliopsida from GBIF

In order to request a download to GBIF using rgbif (Chamberlain, 2017), you need to pass to the function occ_download the following details from your GBIF profile: user name, password, and email. You can do that by editing your R options as shown below.

library(rgbif)
### DOWNLOAD GBIF DATA
options(gbif_user="YOUR USER NAME HERE")
options(gbif_pwd="YOUR PASSWORD HERE")
options(gbif_email="YOUR EMAIL HERE")

1.1 Records of Magnoliopsida

Request download of Magnoliopsida to GBIF

splist <- c('Magnoliopsida') # Select taxonomic group
splist
keys <- sapply(splist, function(x) name_suggest(x)$key[1], USE.NAMES=FALSE)
keys
Mag<-occ_download('taxonKey = 220', 
                   'hasCoordinate = TRUE',
                   'basisOfRecord = PRESERVED_SPECIMEN',
                   'hasGeospatialIssue = FALSE',
                   'decimalLatitude > -18', 'decimalLatitude < 6', 
                   'decimalLongitude > -74', 'decimalLongitude < -45', type = 'and')
                   

Download records of Magnoliopsida and DOI for your requested dataset

my_download_metadata<-occ_download_meta(Mag)
my_download<-occ_download_get("0000632-180508205500799",overwrite=TRUE)
citation_gbif<-gbif_citation(my_download_metadata)

setwd("C:/Users/juliana.stropp/Dropbox (UFAL-ECO)/R_scripts/AMZ_temporal_degradation/data/Magnoliopsida_GBIF")
unzip("C:/Users/juliana.stropp/Dropbox (UFAL-ECO)/R_scripts/AMZ_temporal_degradation/data/Magnoliopsida_GBIF/0000632-180508205500799.zip")

1.2 Records of Liliopsida

splist <- c('Liliopsida')
splist
keys <- sapply(splist, function(x) name_suggest(x)$key[1], USE.NAMES=FALSE)
keys
?occ_download

Lili<-occ_download('taxonKey = 196', 
                  'hasCoordinate = TRUE',
                  'basisOfRecord = PRESERVED_SPECIMEN',
                  'hasGeospatialIssue = FALSE',
                  'decimalLatitude > -18', 'decimalLatitude < 6', 
                  'decimalLongitude > -74', 'decimalLongitude < -45', type = 'and')

Download records of Liliopsida and DOI for your requested dataset

my_download_metadata<-occ_download_meta(Lili)
my_download_metadata
my_download<-occ_download_get("0002013-180508205500799",overwrite=TRUE)

citation_gbif<-gbif_citation(my_download_metadata)
citation_gbif
setwd("C:/Users/juliana.stropp/Dropbox (UFAL-ECO)/R_scripts/AMZ_temporal_degradation/data/Liliopsida_GBIF")
unzip("C:/Users/juliana.stropp/Dropbox (UFAL-ECO)/R_scripts/AMZ_temporal_degradation/data/Liliopsida_GBIF/0002013-180508205500799.zip")

2. Join the two datasets: Magnoliopsida and Liliopsida

Merge the two datasets retrieved from GBIF: Magnoliopsida and Liliopsida

library(data.table)
### Load occurrences of Magnoliopsida
setwd("C:/Users/juliana.stropp/Dropbox (UFAL-ECO)/R_scripts/AMZ_temporal_degradation/data/Magnoliopsida_GBIF")

Mag<-fread("occurrence.txt", na.strings=NULL, sep = '\t',
          stringsAsFactors=FALSE, 
          dec = ".",
          encoding = "UTF-8",
          colClasses = NA_character_,
          header="auto", strip.white = TRUE, data.table = FALSE, 
          integer64=getOption("datatable.integer64"),
          verbose = TRUE)

### Load occurrences of Liliopsida
setwd("C:/Users/juliana.stropp/Dropbox (UFAL-ECO)/R_scripts/AMZ_temporal_degradation/data/Liliopsida_GBIF")
Lili<-fread("occurrence.txt", na.strings=NULL, sep = '\t',
           stringsAsFactors=FALSE, 
           dec = ".",
           encoding = "UTF-8",
           colClasses = NA_character_,
           header="auto", strip.white = TRUE, data.table = FALSE, 
           integer64=getOption("datatable.integer64"),
           verbose = TRUE)
           
### Bind the two datasets
df_GBIF<-rbind(Mag, Lili)

3. Select records only within your study area

To select only records that fall within your study area, you will need to 1) covert your dataframe of records of species occurrence to a SpatialPointsDataFrame, 2) overlay your species occurrence records with the geographical limits of your study area, and 3) select only records that fall within your study area.

3.1 Convert dataframe to SpatialPointsDataFrame

library(rgdal)
lon<-df_GBIF[,c('decimalLongitude')]
lat<-df_GBIF[,c('decimalLatitude')]
coords<-SpatialPoints(cbind(lon,lat))
spdf_GBIF <- SpatialPointsDataFrame(coords, df_GBIF)
proj4string(spdf_GBIF) = CRS("+proj=longlat +datum=WGS84 +no_defs")

3.2 Load shapefile with the geographical limits of your study area

We will use the gepgraphical boundaries of the Brazilian Amazon (source: http://mapas.mma.gov.br/i3geo/datadownload.htm)

AMZ<-readOGR(dsn="C:/Users/juliana.stropp/Dropbox (UFAL-ECO)/R_scripts/AMZ_temporal_degradation/spatial_data",layer="amazlegal") # Source IBGE
plot(AMZ)
proj4string(AMZ) = CRS("+proj=longlat +datum=WGS84 +no_defs")

3.3 Select only records that fall within your study area

Now, we will: 1) overlay your species occurrence records with the geographical limits of your study area, 2) select only records that fall within your study area, 3) convert SpatialPointDataframe to data.frame, and 4) save only records within the study area as .rda file

inside.AMZ <- !is.na(over(spdf_GBIF, as(AMZ, "SpatialPolygons"))) # identify points that overlaps with the study area
inside.AMZ2<- spdf_GBIF[which(inside.AMZ),] # select points that overlaps with the study area
head(inside.AMZ2)
# Convert to dataframe and save as .rda

### Convert to dataframe and save as .rda file
gbif_AMZ<-as.data.frame(inside.AMZ2)
setwd("C:/Users/juliana.stropp/Dropbox (UFAL-ECO)/R_scripts/AMZ_temporal_degradation/data")
save(gbif_AMZ, file = "gbif_AMZ.rda"

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.