Objectives for this section:
.shp
, sf
)ggplot
and geom_sf
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
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.
{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")
Pretty cool! Note, we can open this in our default web browser by clicking on the little box and arrow to expand and view.
{mapview}
There are lots of customization options within {mapview}
, I recommend checking the mapview page to find out more. Here are a few handy tips to change the color, the size, and layer name.
mapview(df_sf,
col.regions="salmon",
cex=3,
layer.name="CSCI Sites")
There a few options to do this. The best option currently is to click on the export button and save as a webpage.
{ggplot}
Alternatively, we can use ggplot2
instead. This is where sf
objects are really nice. They fit well within the ggplot framework because they are simply dataframes with a geometry
list-column at the end. You can plot the X/Y data as part of a geom_point
layer, or you can use the geom_sf
function.
geom_sf
Let’s make a map of all the CSCI stations in California. Let’s also add a scale bar and north arrow with the handy ggspatial
package
library(ggspatial)
nicemap<-
ggplot() + # set up the framework
# use GEOM_SF
geom_sf(data = CA, color="gray", lwd=2) + # add the state outline using geom_sf
# use GEOM_POINT: note, we could use geom_sf instead here too!
geom_point(data=df_sf, aes(x=lon, y=lat), fill="orange", pch=21, alpha=0.7, size=2)+
# scale bar & north arrow
ggspatial::annotation_north_arrow(location="tr") +
ggspatial::annotation_scale() +
# formatting
labs(x="Longitude (WGS84)", y="Latitude", title="Map of CSCI Sites") +
theme_bw() # change this to sans if it doesn't plot
nicemap
# To save plot
# ggsave(filename = "figures/site_map_ggplot.png", width = 8, height = 6, units = "in", dpi = 300)
Finally, let’s make a dynamic map that shows the CSCI scores!
mapview(eldor_pts, zcol="CSCI", layer="CSCI")
# add another layer by linking with "+"
mapview(eldor_co, layer="El Dorado County") +
mapview(eldor_pts, zcol="CSCI", layer="CSCI")
Challenge: Can you make the same map as below using ggplot? Hint,
viridis
may be useful here…
There are loads of resources…but a geospatial workshop you may want to check out is run by the Carpentries. Lessons/material are freely available here: