Session Ready
Exercise

Create a bounding box around vector data

You can compute bounding boxes around vector data using sf. These can help you, for example, create polygons to clip layers to a common area for an analysis or identify regions of influence.

In the sf package, there is a function for extracting the bounding box coordinates, if that's all you need, this is st_bbox(). More likely you'll want to create a new sf object (a polygon) from those coordinates and to do this sf provides the st_make_grid() function.

st_make_grid() can be used to make a multi-row and multi-column grid covering your input data but it can also be used to make a grid of just one cell (a bounding box). To do this, you need to specify the number of grid cells as n = 1.

Instructions
100 XP
  • Plot the beech trees object on top of the neighborhoods object -- wrap neighborhoods in st_geometry() so you're not plotting any attributes. Both have been preloaded. This requires two calls to plot() and one will need add = TRUE.
  • Compute the bounding box coordinates with st_bbox().
  • Create a single polygon bounding box around the beech trees with st_make_grid() with an argument of n = 1.
  • Create a plot of the neighborhoods (just the geometry), the beech trees and the new box around the beech trees.