This lesson is being piloted (Beta version)

Introduction to Geospatial Raster and Vector Data with R: References

Key Points

Intro to Raster Data in R
  • The GeoTIFF file format includes metadata about the raster data.

  • To plot raster data with the ggplot2 package, we need to convert it to a dataframe.

  • R stores CRS information in the Proj4 format.

  • Be careful when dealing with missing or bad data values.

Plot Raster Data in R
  • Continuous data ranges can be grouped into categories using mutate() and cut().

  • Use built-in terrain.colors() or set your preferred color scheme manually.

  • Layer rasters on top of one another by using the alpha aesthetic.

Reproject Raster Data in R
  • In order to plot two raster data sets together, they must be in the same CRS.

  • Use the projectRaster() function to convert between CRSs.

Raster Calculations in R
  • Rasters can be computed on using mathematical functions.

  • The overlay() function provides an efficient way to do raster math.

  • The writeRaster() function can be used to write raster data to a file.

Work With Multi-Band Rasters in R
  • A single raster file can contain multiple bands or layers.

  • Use the stack() function to load all bands in a multi-layer raster file into R.

  • Individual bands within a stack can be accessed, analyzed, and visualized using the same functions as single bands.

Open and Plot Shapefiles in R
  • Shapefile metadata include geometry type, CRS, and extent.

  • Load spatial objects into R with the st_read() function.

  • Spatial objects can be plotted directly with ggplot using the geom_sf() function. No need to convert to a dataframe.

Explore and Plot by Shapefile Attributes
  • Spatial objects in sf are similar to standard data frames and can be manipulated using the same functions.

  • Almost any feature of a plot can be customized using the various functions and options in the ggplot2 package.

Plot Multiple Shapefiles in R
  • Use the + operator to add multiple layers to a ggplot.

  • Multi-layered plots can combine raster and vector datasets.

  • Use the show.legend argument to set legend symbol types.

  • Use the scale_fill_manual() function to set legend colors.

Handling Spatial Projection & CRS in R
  • ggplot2 automatically converts all objects in a plot to the same CRS.

  • Still be aware of the CRS and extent for each object.

Convert from .csv to a Shapefile in R
  • Know the projection (if any) of your point data prior to converting to a spatial object.

  • Convert a data frame to an sf object using the st_as_sf() function.

  • Export an sf object as text using the st_write() function.

Manipulate Raster Data in R
  • Use the crop() function to crop a raster object.

  • Use the extract() function to extract pixels from a raster object that fall within a particular extent boundary.

  • Use the extent() function to define an extent.

Raster Time Series Data in R
  • Use the list.files() function to get a list of filenames matching a specific pattern.

  • Use the facet_wrap() function to create multi-paneled plots with ggplot2.

  • Use the as.Date() function to convert data to date format.

Create Publication-quality Graphics
  • Use the theme_void() function for a clean background to your plot.

  • Use the element_text() function to adjust text size, font, and position.

  • Use the brewer.pal() function to create a custom color palette.

  • Use the gsub() function to do pattern matching and replacement in text.

Derive Values from Raster Time Series
  • Use the cellStats() function to calculate summary statistics for cells in a raster object.

  • The pipe (|) operator means or.

  • Use the rbind() function to combine data frames that have the same column names.

References