This lesson is still being designed and assembled (Pre-Alpha version)

Day 2: Introduction to Raster Data

Overview

Teaching: min
Exercises: 5 min
Questions
  • What are the coordinate reference systems for your rasters and are they the same?

Objectives
  • Visually explore the patterns in your rasters. Reproject the your raster into UTM coordinates.

Outline

In this capstone lesson, we use the following capstone data:

If you are using your own data, please identify two (2) rasters with continuous values and that are unprojected.

1) Load and summarize

Practice the first steps of using data by loading in the maximum and minimum temperature rasters for Wisconsin using the raster() function and then exploring using the summary() function.

Solution

library(raster)
Loading required package: sp
library(ggplot2)
library(rgdal)
Error: package or namespace load failed for 'rgdal' in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/home/runner/work/_temp/Library/rgdal/libs/rgdal.so':
  libgdal.so.26: cannot open shared object file: No such file or directory
mintemp <- raster("../data/mintemp_monthcold_wi.tif")
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create RasterLayer object from this file; perhaps you need to install rgdal first
summary(mintemp)
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': object 'mintemp' not found
maxtemp <- raster("../data/maxtemp_monthwarm_wi.tif")
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create RasterLayer object from this file; perhaps you need to install rgdal first
summary(maxtemp)
Error in h(simpleError(msg, call)): error in evaluating the argument 'object' in selecting a method for function 'summary': object 'maxtemp' not found

2) Check the CRS

Use the crs() function to check if the coordinate reference systems the same in these two rasters?

Solution

mintemp
Error in eval(expr, envir, enclos): object 'mintemp' not found
maxtemp
Error in eval(expr, envir, enclos): object 'maxtemp' not found

OR

crs(mintemp)
Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'crs': object 'mintemp' not found
crs(maxtemp)
Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'crs': object 'maxtemp' not found

Yes, the CRS for both rasters is the same. Both are in longlat projection, datum WGS84.

3) Visualize

Create visualizations of both minimum and maximum temperature rasters using the ggplot2 package.

Solution

mintemp_df <- as.data.frame(mintemp, xy=TRUE)
Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'as.data.frame': object 'mintemp' not found
maxtemp_df <- as.data.frame(maxtemp, xy=TRUE)
Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'as.data.frame': object 'maxtemp' not found
ggplot() +
   geom_raster(data = mintemp_df , aes(x = x, y = y, fill = mintemp_monthcold_wi)) +
   scale_fill_viridis_c() +
   coord_quickmap()
Error in fortify(data): object 'mintemp_df' not found
ggplot() +
   geom_raster(data = maxtemp_df , aes(x = x, y = y, fill = maxtemp_monthwarm_wi)) +
   scale_fill_viridis_c() +
   coord_quickmap()
Error in fortify(data): object 'maxtemp_df' not found

Bonus

You can add the following aesthetics to a ggplot to customize the backgrounds, axis labels, title, legend label, and move the legend position to the bottom of the plot:

ggplot() +
    geom_*() +
    labs(title = "Your Title", x="X Label", y="Y Label", fill="Legend Label") +
    theme_bw() +
    theme(plot.title = element_text(hjust = 0.5), legend.position = "bottom")

Explore what these aesthetics do with your plot of maximum monthly temperature.

Solution

ggplot() +
   geom_raster(data = maxtemp_df , aes(x = x, y = y, fill = maxtemp_monthwarm_wi)) +
   scale_fill_viridis_c() +
   coord_quickmap() +
   labs(title = "Maximum Monthly Temperature: WI", x="Longitude", y="Latitude", fill="Max. Temp (C)") +
   theme_bw() +
   theme(plot.title = element_text(hjust = 0.5), legend.position = "bottom")
Error in fortify(data): object 'maxtemp_df' not found

4) Develop insights

Describe what patterns you observe across the state for coldest and warmest monthly temperatures in Wisconsin. What do you hypothesize is the effect of Lake Michigan?

Solution

Warmer temperatures are in Wisconsin tend toward the lower half of the state and nearer to the Minnesota border. Colder temperatures tend toward the North and near Lake Michigan, possibly due to the lake effect

5) Reproject

Reproject the maxtemp raster into UTM coordinates using the following crs string: ` +proj=utm +zone=15 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 `. This string was obtained from Spatial Reference Orig under PROJ4.

Solution

maxtemp_utm <- projectRaster(maxtemp, crs=" +proj=utm +zone=15 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 ")
Error in .requireRgdal(): package 'rgdal' is not available
crs(maxtemp_utm)
Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'crs': object 'maxtemp_utm' not found
crs(maxtemp)
Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'crs': object 'maxtemp' not found

Key Points

  • Always explore your raster first using the summary() and crs() functions.

  • To plot your raster with ggplot(), first convert your raster to a data frame using the as.data.frame() function and specify the argument xy=TRUE.

  • You may need to reproject your raster, for which you can use the projectRaster() function.

  • There are many plotting and aesthetics options in the ggplot2 package for you to explore.