Intro to Raster Data


  • 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


  • 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 order to plot two raster data sets together, they must be in the same CRS.
  • Use the project() function to convert between CRSs.

Raster Calculations


  • Rasters can be computed on using mathematical functions.
  • The lapp() 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


  • A single raster file can contain multiple bands or layers.
  • Use the rast() function to load all bands in a multi-layer raster file into R.
  • Individual bands within a SpatRaster can be accessed, analyzed, and visualized using the same functions no matter how many bands it holds.

Open and Plot Vector Layers


  • Metadata for vector layers 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 Vector Layer 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 Vector Layers


  • 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


  • 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 Vector Layer


  • 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


  • 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 ext() function to define an extent.

Raster Time Series Data


  • 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 global() 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.