Introduction To Geospatial Raster and Vector Data With R - Open and Plot Shapefiles in R
Introduction To Geospatial Raster and Vector Data With R - Open and Plot Shapefiles in R
Teaching: 20 min
Exercises: 10 min
Questions
How can I distinguish between and visualize point, line and polygon vector data?
Objectives
Know the difference between point, line, and polygon vector elements.
Load point, line, and polygon shapefiles into R.
Access the attributes of a spatial object in R.
https://datacarpentry.org/r-raster-vector-geospatial/06-vector-open-shapefile-in-r/ 1/9
23/10/2020 Introduction to Geospatial Raster and Vector Data with R: Open and Plot Shapefiles in R
Starting with this episode, we will be moving from working with raster data to working with vector data. In this
episode, we will open and plot point, line and polygon vector data stored in shapefile format in R. These data refer to
the NEON Harvard Forest field site (https://www.neonscience.org/field-sites/field-sites-map/HARV), which we have
been working with in previous episodes. In later episodes, we will learn how to work with raster and vector data
together and combine them into a single plot.
Import Shapefiles
We will use the sf package to work with vector data in R. Notice that the rgdal package automatically loads
when sf is loaded. We will also use the raster package, which has been loaded in previous episodes, so we can
explore raster and vector spatial metadata using similar commands. Make sure you have the sf library loaded.
library(sf)
The first shapefile that we will open contains the boundary of our study area (or our Area Of Interest or AOI, hence
the name aoiBoundary ). To import shapefiles we use the sf function st_read() . st_read() requires the file
path to the shapefile.
https://datacarpentry.org/r-raster-vector-geospatial/06-vector-open-shapefile-in-r/ 2/9
23/10/2020 Introduction to Geospatial Raster and Vector Data with R: Open and Plot Shapefiles in R
Data Tip
Spatial Metadata
Key metadata for all shapefiles include:
We can view shapefile metadata using the st_geometry_type() , st_crs() and st_bbox() functions. First, let’s
view the geometry type for our AOI shapefile:
st_geometry_type(aoi_boundary_HARV)
[1] POLYGON
18 Levels: GEOMETRY POINT LINESTRING POLYGON MULTIPOINT ... TRIANGLE
Our aoi_boundary_HARV is a polygon object. The 18 levels shown below our output list the possible categories of
the geometry type. Now let’s check what CRS this file data is in:
st_crs(aoi_boundary_HARV)
Our data in the CRS UTM zone 18N. The CRS is critical to interpreting the object’s extent values as it specifies
units. To find the extent of our AOI, we can use the st_bbox() function:
https://datacarpentry.org/r-raster-vector-geospatial/06-vector-open-shapefile-in-r/ 4/9
23/10/2020 Introduction to Geospatial Raster and Vector Data with R: Open and Plot Shapefiles in R
st_bbox(aoi_boundary_HARV)
The spatial extent of a shapefile or R spatial object represents the geographic “edge” or location that is the furthest
north, south east and west. Thus is represents the overall geographic coverage of the spatial object. Image Source:
National Ecological Observatory Network (NEON).
Lastly, we can view all of the metadata and attributes for this shapefile object by printing it to the screen:
aoi_boundary_HARV
https://datacarpentry.org/r-raster-vector-geospatial/06-vector-open-shapefile-in-r/ 5/9
23/10/2020 Introduction to Geospatial Raster and Vector Data with R: Open and Plot Shapefiles in R
Plot a Shapefile
Next, let’s visualize the data in our sf object using the ggplot package. Unlike with raster data, we do not need to
convert vector data to a dataframe before plotting with ggplot .
We’re going to customize our boundary plot by setting the size, color, and fill for our plot. When plotting sf objects
with ggplot2 , you need to use the coord_sf() coordinate system.
ggplot() +
geom_sf(data = aoi_boundary_HARV, size = 3, color = "black", fill = "cyan1") +
ggtitle("AOI Boundary Plot") +
coord_sf()
https://datacarpentry.org/r-raster-vector-geospatial/06-vector-open-shapefile-in-r/ 6/9
23/10/2020 Introduction to Geospatial Raster and Vector Data with R: Open and Plot Shapefiles in R
https://datacarpentry.org/r-raster-vector-geospatial/06-vector-open-shapefile-in-r/ 7/9
23/10/2020 Introduction to Geospatial Raster and Vector Data with R: Open and Plot Shapefiles in R
Using the steps above, import the HARV_roads and HARVtower_UTM18N layers into R. Call the
HARV_roads object lines_HARV and the HARVtower_UTM18N point_HARV .
Answer the following questions:
1. What type of R spatial object is created when you import each layer?
2. What is the CRS and extent for each object?
3. Do the files contain points, lines, or polygons?
4. How many spatial objects are in each file?
Answers
Key Points
(../05- (../07-
raster- vector-
multi- shapefile-
band- attributes-
in- in-
r/index.html) r/index.html)
https://datacarpentry.org/r-raster-vector-geospatial/06-vector-open-shapefile-in-r/ 8/9
23/10/2020 Introduction to Geospatial Raster and Vector Data with R: Open and Plot Shapefiles in R
https://datacarpentry.org/r-raster-vector-geospatial/06-vector-open-shapefile-in-r/ 9/9