Introduction to Working With Vector Data in R feature image Source: National Ecological Observatory Network (NEON)


Devoted to open data and open source in science and education.

View All Tutorials


R programming (56)
Hierarchical Data Formats (HDF5) (15)
Spatial Data & GIS (22)
LiDAR (10)
Raster Data (14)
Remote Sensing (25)
Data Visualization (4)
Hyperspectral Remote Sensing (18)
Time Series (17)
Phenology (8)
Vector Data (6)
Metadata (1)
Git & GitHub (7)
(1) (1) (14) (1) (1) (1) (1)

Tutorial by R Package

dplyr (9)
ggplot2 (18)
h5py (2)
lubridate (time series) (7)
maps (1)
maptools (1)
plyr (2)
raster (26)
rasterVis (raster time series) (3)
rgdal (GIS) (24)
rgeos (2)
rhdf5 (11)
sp (5)
scales (4)
gridExtra (4)
ggtheme (0)
grid (2)
reshape2 (3)
plotly (5)

View ALL Tutorial Series

Twitter Youtube Github


R Bloggers

Series: Introduction to Working With Vector Data in R


The data tutorials in this series cover how to open, work with and plot vector-format spatial data (points, lines and polygons) in R. Additional topics include working with spatial metadata (extent and coordinate reference system), working with spatial attributes and plotting data by attribute.

Data used in this series cover NEON Harvard Forest Field Site and are in shapefile and .csv formats.

R Skill Level: Intermediate - you’ve got the basics of R down but haven’t previously worked with spatial data in R.

Series Goals / Objectives

After completing the series you will:

  • Vector 00:
    • Know the difference between point, line, and polygon vector elements.
    • Understand the differences between opening point, line and polygon shapefiles in R.
  • Vector 01:
    • Understand the components of a spatial object in R.
    • Be able to query shapefile attributes.
    • Be able to subset shapefiles using specific attribute values.
    • Know how to plot a shapefile, colored by unique attribute values.
  • Vector 02:
    • Be able to plot multiple shapefiles using base plot().
    • Be able to apply custom symbology to spatial objects in a plot in R.
    • Be able to customize a baseplot legend in R.
  • Vector 03:
    • Know how to identify the CRS of a spatial dataset.
    • Be familiar with geographic vs. projected coordinate reference systems.
    • Be familiar with the proj4 string format which is one format used used to store / reference the CRS of a spatial object.
  • Vector 04:
    • Be able to import .csv files containing x,y coordinate locations into R.
    • Know how to convert a .csv to a spatial object.
    • Understand how to project coordinate locations provided in a Geographic Coordinate System (Latitude, Longitude) to a projected coordinate system (UTM).
    • Be able to plot raster and vector data in the same plot to create a map.
  • Vector 05:
    • Be able to crop a raster to the extent of a vector layer.
    • Be able to extract values from raster that correspond to a vector file overlay.

Things You’ll Need To Complete This Series

Setup RStudio

To complete the tutorial series you will need an updated version of R and, preferably, RStudio installed on your computer.

R is a programming language that specializes in statistical computing. It is a powerful tool for exploratory data analysis. To interact with R, we strongly recommend RStudio, an interactive development environment (IDE).

Install R Packages

You can chose to install packages with each lesson or you can download all of the necessary R packages now.

  • raster: install.packages("raster")
  • rgdal: install.packages("rgdal")
  • sp: install.packages("sp")

More on Packages in R - Adapted from Software Carpentry.

Download Data

Download NEON Teaching Data Subset: Site Layout Shapefiles

These vector data provide information on the site characterization and infrastructure at the National Ecological Observatory Network’s Harvard Forest field site. The Harvard Forest shapefiles are from the Harvard Forest GIS & Map archives. US Country and State Boundary layers are from the US Census Bureau.

Download NEON Teaching Data Subset: Airborne Remote Sensing Data

The LiDAR and imagery data used to create this raster teaching data subset were collected over the National Ecological Observatory Network’s Harvard Forest and San Joaquin Experimental Range field sites and processed at NEON headquarters. The entire dataset can be accessed by request from the NEON Airborne Data Request Page on the NEON website.

Set Working Directory: This lesson assumes that you have set your working directory to the location of the downloaded and unzipped data subsets. An overview of setting the working directory in R can be found here.

R Script & Challenge Code: NEON data lessons often contain challenges that reinforce learned skills. If available, the code for challenge solutions is found in the downloadable R script of the entire lesson, available in the footer of each lesson page.

Working with Spatio-temporal Data in R Series: This tutorial series is part of a larger spatio-temporal tutorial series and Data Carpentry workshop. Included series are introduction to spatio-temporal data and data management, working With raster data in R, working with vector data in R and working with tabular time series in R.

Tutorials in the Series