Up to this point, we have dealt with data that fits into the tidy format without much effort. Spatial data has many complicating factors that have made handling spatial data in R complicated. Big strides are being made to make spatial data tidy in R.
You woke up in the middle of the night terrified of the Canadians after a bad dream. You decide you need to set up military bases to defend the Canada-NY border. After you tweet your plans, you realize you have no plan. What will you do next?
- Generate a polygon that includes all land in NY that is within 10km of the Canadian border (not including the great lakes).
- Calculate it’s area in km^2. How much land will you need to defend from the Canadians?
st_transform()
Download starter R script (if desired)
The details below describe one possible approach.
You will need to load the following packages
library(spData)
library(sf)
library(tidyverse)
# library(units) #this one is optional, but can help with unit conversions.
#load 'world' data from spData package
data(world)
# load 'states' boundaries from spData package
data(us_states)
# plot(world[1]) #plot if desired
# plot(us_states[1]) #plot if desired
world
dataset
albers="+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
it easier to use ggplot()
name_long=="Canada"
us_states
object
albers
us_states
dataset to include only
NAME == "New York"
st_intersection()
to intersect the canada buffer
with New York (this will be your final polygon)ggplot()
and
geom_sf()
.st_area()
to calculate the area of this
polygon.set_units(km^2)
(from the units
library) or some other method.Your final result should look something like this:
Important note: This is a crude dataset meant simply to illustrate the use of intersections and buffers. The two datasets are not adequate for a highly accurate analysis. Please do not use these data for real military purposes.
Build a leaflet map of the same dataset.