Downloading occurrence data from GBIF and plotting on a map

## Loading required package: raster
## Loading required package: sp
## rgdal: version: 1.2-18, (SVN revision 718)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.3, released 2015/09/16
##  Path to GDAL shared files: /usr/share/gdal/1.11
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
##  Path to PROJ.4 shared files: (autodetected)
##  Linking to sp version: 1.2-7

Go to www.naturalearthdata.com

Click: Get the Data > Medium Scale Data; Cultural > Download countries.
Load the shapefile back into R.
continents <- readOGR(dsn = "/home/pjg/GIS/shpData", layer = "ne_50m_admin_0_countries")
## OGR data source with driver: ESRI Shapefile 
## Source: "/home/pjg/GIS/shpData", layer: "ne_50m_admin_0_countries"
## with 241 features
## It has 71 fields
##### Subset the SpatialPolygonsDataset by North America. Try plotting this and see how it looks. 
N.America<-continents[continents@data$CONTINENT=="North America",]
##### crop North America by a rough extent of the area of interest.
Amb.opa<-crop(N.America, extent(c(-100, -66, 23, 46)))
## Loading required namespace: rgeos
# Plot this extent of the map. Use any color you like.
plot(Amb.opa, col='blue')

Query GBIF for data associated with species name

MarbSalam<- gbif(genus = "Ambystoma", species = "opacum", download = T)
## Loading required namespace: jsonlite
## 9414 records found
## 0-300-600-900-1200-1500-1800-2100-2400-2700-3000-3300-3600-3900-4200-4500-4800-5100-5400-5700-6000-6300-6600-6900-7200-7500-7800-8100-8400-8700-9000-9300-9414 records downloaded
# Look at the first 5 rows of MarbSalam. Get only the columns that have the species name, latitude and longitude.
Locs<-na.omit(data.frame(cbind(MarbSalam$species), MarbSalam$lon, MarbSalam$lat))
# Rename the column names of the Locs data.frame
colnames(Locs)<-c("SPECIES","LONGITUDE","LATITUDE")
# Plot the previous shapefile
plot(Amb.opa, col='blue')
# Put these points on the map
points(Locs[,2:3], col='red', pch=16)
# Add a legend
legend("bottomright", col='red', pch=16, legend = 'Ambystoma opacum')
# Add a scale
map.scale(x = -98, y = 23, relwidth=0.1, metric=T)
# load GISTools library. This will mask the map.scale function from the "maps" package. Do this step last.
# Add north arrow.
library(GISTools)
## Loading required package: maptools
## Checking rgeos availability: TRUE
## Loading required package: RColorBrewer
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## 
## The following objects are masked from 'package:raster':
## 
##     area, select
## 
## Loading required package: rgeos
## rgeos version: 0.3-26, (SVN revision 560)
##  GEOS runtime version: 3.5.0-CAPI-1.9.0 r4084 
##  Linking to sp version: 1.2-5 
##  Polygon checking: TRUE 
## 
## 
## Attaching package: 'GISTools'
## 
## The following object is masked from 'package:maps':
## 
##     map.scale
north.arrow(xb=-67, yb = 30, len=0.5, lab="N")