Objectives for this section:

  • Make dynamic interactive webmaps
  • Read in several spatial layers (.shp, sf)
  • Make a map with ggplot and geom_sf


Mapping In R

Load packages

First let’s load the packages we’ll be using.

# load packages or "libraries"
library(tidyverse) # wrangling/plotting tools
library(sf) # newer "simple features" spatial package
library(mapview) # interactive web mapping
library(tmap) # static mapping


Load Data

First we need to import some data. We’ll be using the data we saved from the sf lesson. Since we are following the data management/organization tips, we are in an RStudio Project, and we have a data folder with our data file inside!

# This is points and El Dorado County Data
load("data/m2_3_out_eldorado_sf.rda")

# this is state boundaries
states <- st_read("data/states_boundaries.shp",
        stringsAsFactors = FALSE, 
        as_tibble = TRUE)
## Reading layer `states_boundaries' from data source `/Users/ryanpeek/Documents/github/TEACHING/2020_CABW_R_training/data/states_boundaries.shp' using driver `ESRI Shapefile'
## Simple feature collection with 3 features and 12 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -124.5662 ymin: 32.53416 xmax: -114.0396 ymax: 46.29204
## geographic CRS: WGS 84
# filter to just CA
CA <- filter(states, name=="California")

So we have most of our pieces from the previous lesson, including the CSCI point data, the El Dorado county line and buffer, and the points that only fell inside either the buffer or the county.


Plotting with Interactive Maps: {mapview}

One of the easiest and coolest packages you’ll find for interactive mapping is the {mapview} package. As long as data are in an sf format, you can quickly make an interactive map with your data. First let’s make sure we have an sf class of data.

# check data is in sf format?
class(df_sf)
## [1] "sf"         "tbl_df"     "tbl"        "data.frame"

Next we can use the simple mapview() function to see a default interactive webmap!

# make a map with mapview
mapview::mapview(df_sf, layer="CSCI Sites")