Introduction to Working with Raster Data in R

Creation supported by NEON, Data Carpentry, SESYNC, and iPlant Collaborative
Table of Contents

The tutorials in this series cover how to open, work with and plot raster-format spatial data in R. Additional topics include working with spatial metadata (extent and coordinate reference system), reprojecting spatial data and working with raster time series data.

Data used in this series cover NEON Harvard Forest and San Joaquin Experimental Range field sites and are in GeoTIFF and .csv formats.

Learning Objectives

After completing the series you will:

  • Raster 00
    • Understand what a raster dataset is and its fundamental attributes.
    • Know how to explore raster attributes in R.
    • Be able to import rasters into R using the raster package.
    • Be able to quickly plot a raster file in R.
    • Understand the difference between single- and multi-band rasters.
  • Raster 01
    • Know how to plot a single band raster in R.
    • Know how to layer a raster dataset on top of a hillshade to create an elegant basemap.
  • Raster 02
    • Be able to reproject a raster in R.
  • Raster 03
    • Be able to to perform a subtraction (difference) between two rasters using raster math.
    • Know how to perform a more efficient subtraction (difference) between two rasters using the raster overlay() function in R.
  • Raster 04
    • Know how to identify a single vs. a multi-band raster file.
    • Be able to import multi-band rasters into R using the raster package.
    • Be able to plot multi-band color image rasters in R using plotRGB.
    • Understand what a NoData value is in a raster.
  • Raster 05
    • Understand the format of a time series raster dataset.
    • Know how to work with time series rasters.
    • Be able to efficiently import a set of rasters stored in a single directory.
    • Be able to plot and explore time series raster data using the plot() function in R.
  • Raster 06
    • Be able to assign custom names to bands in a RasterStack for prettier plotting.
    • Understand advanced plotting of rasters using the rasterVis package and levelplot.
  • Raster 07
    • Be able to extract summary pixel values from a raster.
    • Know how to save summary values to a .csv file.
    • Be able to plot summary pixel values using ggplot().
    • Have experience comparing NDVI values between two different sites.

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")
  • rasterVis: install.packages("rasterVis")
  • ggplot2: install.packages("ggplot2")

  • More on Packages in R – Adapted from Software Carpentry.

Working with Raster Data in R Tutorial Series: This tutorial series is part of a Data Carpentry workshop
on using spatio-temporal in R. Other related series include: intro to spatio-temporal data and data management, working with vector data in R, and working with tabular time series data in R.


There are errors in the R script file with the file names. Use of capitals and non-capitalisation in different places.

Hi aegerton. Thank you for your comment on this series -- we do want to make sure all the resources are usable. I've gone through all the downloadable R scripts in this series and am unable to replicate your errors with the file names. If you, or someone else who encounters this error, could you indicate which R script is involved, we will go ahead and update it. Thank you.

I get the following error when I try to overlay the DTM on top of the hillshade
Error in graphics::rasterImage(x, e[1], e[3], e[2], e[4], interpolate = interpolate) :
cannot allocate memory block of size 67108864 Tb

Sarah -- Your error has to do with a memory issue. Do you have other large processes running or R objects in the system? Alternatively, have you modified the lesson in any way so that you may by overlaying non-subsetted layer? If you are still encountering these errors, please let us know a few more details and we may be able to figure out what is going wrong.

What is the use of $ sign while searching for tif file? Example: Get started section at the beginning

all_HARV_NDVI <- list.files("NEON-DS-Landsat-NDVI/HARV/2011/NDVI",
full.names = TRUE,
pattern = ".tif$")

Madhur, The $ indicates the end of the string in a regular expression. This StackOverflow question is an nice example of when you might use it:

Leave a comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Find more series
Dialog content.