Working with bcmaps layers with point location data

2020-04-28

We often want to be able to add point data to a map. This vignette will run through a simple example of converting a data.frame of latitude/longitude pairs into a SpatialPointsDataFrame, and reprojecting it so that the points can be overlaid on a layer from the bcmaps package.

For this example, say we have done surveys for a species across B.C., and we want to be able to add the presences and absences on to a map of British Columbia.

First, load the packages we will need:

library(sf)
library(bcmaps)

We will create a mock dataframe of locations of species presence/absences (in real life these would probably be in a csv or Excel file that we would import):

set.seed(42)
spp <- data.frame(site_num = LETTERS[1:10], spp_present = sample(c("yes", "no"), 10, replace = TRUE),
                 lat = runif(10, 49, 60), long = runif(10, -128, -120), 
                 stringsAsFactors = FALSE)
head(spp)
##   site_num spp_present      lat      long
## 1        A         yes 54.03516 -120.7677
## 2        B         yes 56.91023 -126.8903
## 3        C         yes 59.28139 -120.0889
## 4        D         yes 51.80972 -120.4267
## 5        E          no 54.08522 -127.3405
## 6        F          no 59.34016 -123.8863

Next we convert this to a SpatialPointsDataFrame using the sp package. The syntax for doing this is a little obscure - you specify the columns that hold the x and y data in a character vector on the right hand side of the <-:

spp <- st_as_sf(spp, coords = c("long", "lat"))
summary(spp)
##    site_num         spp_present           geometry 
##  Length:10          Length:10          POINT  :10  
##  Class :character   Class :character   epsg:NA: 0  
##  Mode  :character   Mode  :character
plot(spp["spp_present"])

In order to overlay these points on other spatial layers, they need to use the same Coordinate Reference System (CRS). Coordinate systems and projections in R can be confusing. There is a great reference on using them here: https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/OverviewCoordinateReferenceSystems.pdf.

We know that our occurrence data are in decimal degrees in NAD83, so we will assign the corresponding proj4string. You can specify the projection using a full proj4 string ("+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs") or the EPSG code (4269). We will use the EPSG code because it’s shorter and less likely to make a typo with.

spp <- st_set_crs(spp, 4326)

All of the maps in the bcmaps package are in BC Albers projection (EPSG:3005), which is the B.C. government standard. It is an ‘equal area’ projection, meaning that the size of areas is not distorted, and thus is suitable for analyses on large areas.

If we look at the proj4string for bc_bound and our spp_df, we see that they are different:

bc_bound <- get_layer("bc_bound")
st_crs(bc_bound)
## Coordinate Reference System:
##   User input: EPSG:3005 
##   wkt:
## PROJCS["NAD83 / BC Albers",
##     GEOGCS["NAD83",
##         DATUM["North_American_Datum_1983",
##             SPHEROID["GRS 1980",6378137,298.257222101,
##                 AUTHORITY["EPSG","7019"]],
##             TOWGS84[0,0,0,0,0,0,0],
##             AUTHORITY["EPSG","6269"]],
##         PRIMEM["Greenwich",0,
##             AUTHORITY["EPSG","8901"]],
##         UNIT["degree",0.0174532925199433,
##             AUTHORITY["EPSG","9122"]],
##         AUTHORITY["EPSG","4269"]],
##     PROJECTION["Albers_Conic_Equal_Area"],
##     PARAMETER["standard_parallel_1",50],
##     PARAMETER["standard_parallel_2",58.5],
##     PARAMETER["latitude_of_center",45],
##     PARAMETER["longitude_of_center",-126],
##     PARAMETER["false_easting",1000000],
##     PARAMETER["false_northing",0],
##     UNIT["metre",1,
##         AUTHORITY["EPSG","9001"]],
##     AXIS["Easting",EAST],
##     AXIS["Northing",NORTH],
##     AUTHORITY["EPSG","3005"]]
st_crs(spp)
## Coordinate Reference System:
##   User input: EPSG:4326 
##   wkt:
## GEOGCS["WGS 84",
##     DATUM["WGS_1984",
##         SPHEROID["WGS 84",6378137,298.257223563,
##             AUTHORITY["EPSG","7030"]],
##         AUTHORITY["EPSG","6326"]],
##     PRIMEM["Greenwich",0,
##         AUTHORITY["EPSG","8901"]],
##     UNIT["degree",0.0174532925199433,
##         AUTHORITY["EPSG","9122"]],
##     AUTHORITY["EPSG","4326"]]

So let’s transform the dataframe of species presence/absences into the same CRS as bc_bound:

spp <- transform_bc_albers(spp)

Now we can overlay the points on the provincial boundary dataset:

plot(spp["spp_present"], expandBB = rep(0.2, 4), graticule = TRUE)
plot(st_geometry(bc_bound), add = TRUE)

Now we want to know what ecoregion of the province each of these observations was in. We can use the ecoregions data from bcmaps, and the st_join function from the sf package to extract ecoregions from the point data and add that information:

ecoreg <- ecoregions()
st_join(spp, ecoreg["ECOREGION_NAME"])
## Simple feature collection with 10 features and 3 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 912519.3 ymin: 600149.1 xmax: 1383324 ymax: 1642418
## CRS:            EPSG:3005
##    site_num spp_present                   ECOREGION_NAME
## 1         A         yes CENTRAL CANADIAN ROCKY MOUNTAINS
## 2         B         yes    BOREAL MOUNTAINS AND PLATEAUS
## 3         C         yes                HAY-SLAVE LOWLAND
## 4         D         yes               COLUMBIA HIGHLANDS
## 5         E          no       EASTERN HAZELTON MOUNTAINS
## 6         F          no                   MUSKWA PLATEAU
## 7         G          no                   MUSKWA PLATEAU
## 8         H          no        THOMPSON-OKANAGAN PLATEAU
## 9         I         yes                     FRASER BASIN
## 10        J          no CENTRAL CANADIAN ROCKY MOUNTAINS
##                    geometry
## 1   POINT (1341578 1016164)
## 2  POINT (945861.3 1324757)
## 3   POINT (1337001 1602713)
## 4  POINT (1383324 770393.7)
## 5  POINT (912519.3 1009950)
## 6   POINT (1120431 1596971)
## 7   POINT (1063173 1642418)
## 8  POINT (1373357 600149.1)
## 9   POINT (1102484 1025860)
## 10  POINT (1297769 1139361)