Learning Objectives
Following this assignment students should be able to:
- Import existing spatial datasets into R
- Read-in and convert raw data to a spatial data layer
- Manipulate spatial data based on data attributes
- Transform spatial data layers for subsequent analysis
- Combine spatial data layers to produce a map
Reading
-
Topics
- R spatial
-
Readings
- Introduction to Working With Vector Data in R
- Introduction to Working With Raster Data in R
- Spatial Cheatsheet
Exercises
-- Importing Spatial Data --
Your advisor has tasked you with analyzing weather data from the Oklahoma Mesonet for an upcoming presentation. In particular, he is interested in the connection between weather and elevation. Since he knows you’ve been learning awesome data visualization and analysis tools in your scientific computing course he asks you to produce a map that shows the locations of all active Oklahoma Mesonet stations as well as the elevation, climate zones and county boundaries.
Using
install.packages(), install theraster,rgdal, andsppackages.-
You decide to start with a shapefile of the county boundaries. Download the Oklahoma county boundaries shapefile from here and the projection file from here. Unzip both files into your data directory. Use the
readOGRfunction to read thecountylayer into R. Plot it using theplotfunction. -
Next you decide to read in a layer of the climate division boundaries. Download the zipfile of NOAA climate divisions from here and extract it in your data directory. Read in the
GIS.OFFICIAL_CLIM_DIVISIONSlayer and plot it using theplotfunction. -
Now that you’ve got the vector data layers imported, you decide to import a raster file of elevation for Oklahoma. Your friend has already clipped the National Elevation Dataset for Oklahoma agreggated it to a 1 arc-minute resolution, so she sends you a link to download it. Download it into your data folder and import it into R using the
rasterfunction. Plot the resulting data layer.
-
-- Converting Raw Data to Spatial Data --
Your next task is to create a spatial data layer that contains the coordinates for Oklahoma Mesonet sites. You talk to your friend who has worked with Mesonet data before and she tells you about a handy R package called
okmesonetfor accessing Mesonet data. She suggests you use theupdatestnfunction to pull in the station information you need.-
Install the
okmesonetpackage and use theupdatestnfunction to read in a data frame with the station location data. Usesummaryto check the data. -
On inspecting the data frame, you notice that some of the locations in the data frame have already been decommissioned. Because you are only interested in currently active stations, remove the rows for stations that have already been decommissioned. Check the data using
summary. -
Now that you have the proper data, you need to convert your
data.frameinto a spatial data layer. You recall that in order to do this conversion you need to define a coordinate reference system (CRS) for the layer. Rather than create one from scratch you decide to extract the CRS from your county boundaries data layer, since that data set also uses latitude and longitude for coordinates. Use thecrsfunction to extract the CRS from your county boundaries data layer and assign it to an R object. -
Convert your
data.frameto aSpatialPointsDataFrameusing your CRS object and theSpatialPointsDataFramefunction. Plot the resulting spatial data layer.
-
-- Manipulating and Transforming Spatial Data --
Now that you have all the data in R, it’s time to prepare the data for creating your map. In order to create your map you need to have all of your layers clipped to the same extent and using the same projection.
-
Use the
extentfunction to check the geographic extent of the data layers for Oklahoma county boundaries, NOAA climate divisions, Oklahoma elevation, and Oklahoma Mesonet station locations. -
You note that all the layers seem pretty close except for the NOAA climate divisions and you realize that you forgot to subset that layer to include only climate divisions for Oklahoma. Use the
subsetfunction to exclude any polygons that are not in Oklahoma and assign the resultingSpatialPolygonsDataFrameto a new R object. Check the extent of the new data layer. -
Now that the extents are good, you need to check the projections. Use
crsto check the projections of your data layers for Oklahoma county boundaries, Oklahoma climate divisions, Oklahoma elevation, and Oklahoma Mesonet station locations. -
You notice that the output from
crsis different for your layers. To make sure that everything lines up properly you decide to transform each layer to match the CRS as specified for the Oklahoma county boundaries layer. UsespTransformto transform all your vector data layers to the CRS from the Oklahoma boundaries layer. Usecrsto assign the proper CRS specification to your raster layer. Check all CRS specifications usingcrs. -
Plot all data layers starting with Oklahoma elevation. (Hint: use
add=TRUEto overlay each data layer) -
In looking at your preliminary map, you realize that you need to remove the areas of the Oklahoma elevation data layer that are beyond the state boundary. Use the
maskfunction to create a new data layer that contains only values that are within the state boundary. Plot all data layers starting with your new elevation data layer. -
You’re feeling pretty proud of your map so you show it off to your office-mate. He suggests that a hillshade plot for your elevation data might highlight the topography better. After some searching, you find that you can create a hillshade plot using the
hillShadefunction. To do that, you’ll need to use theterrainfunction to calculate the slope and aspect and assign them to objects that you can provide as arguments tohillShade. Create a hillshade data layer using the slope and aspect calculated from the elevation data and plot the result. (Hint: use 45 for theangleand 270 for thedirectionarguments tohillShade)
-
-- Producing a Custom Map --
Your data layers are ready to be combined into your final map.
-
Plot the hillshade data layer.
-
The legend of the hillshade plot is not very informative and somewhat distracting so you decide it can be removed. You also decide that the axes and bounding box are unncessary. Plot the hillshade layer again removing the axes, bounding box, and legend. (Hint: use
box=FALSEto get rid of the bounding box) -
Because you intend to overlay other layers on top of the hillshade layer, you decide it would be better to have the hillshade in grayscale instead of color. Use the
grayfunction and thecolargument toplotto plot the hillshade layer in grayscale. (Hint: using0:100/100for thelevelargument tograyshould give you a nice contrast). -
Add the Oklahoma climate divisions to the map using the
rainbowfunction and thecolargument toplot. Be sure to specify the proper number of colors to correspond to the number of climate divisions when you callrainbow. -
You realize that in order to see the elevation you will need to increase the transparency of the colors for the climate divisions layer. Use the
alphaargument to therainbowfunction to make the colors more transparent and replot the elevation and climate division layers. (Hint: analphavalue of about 0.15 should be about right.) -
Now you are ready to add the Oklahoma county boundaries layer. Use
plotto add this layer to the plot. The county boundaries are really more background context for the map, so use theborderargument to set the color for the boundary lines to “gray”. -
You are now ready to add your Oklahoma Mesonet station locations to your map. Add the Oklahoma Mesonet station layer to your map. Use the
pchargument to set the point character to a filled triangle.
-
