Session Ready
Exercise

Plot vector and raster together

If the layers do not share a common CRS they may not align on a plot. To illustrate, in this exercise, you will initially create a plot with the plot() function and try to add two layers that do not share the same CRS. You will then transform one layer's CRS to match the other and you will plot this with both the plot() function and functions from the tmap package.

Note that for this exercise we returned all the layers to their original CRS and did not retain the changes you made in the last exercise.

With the plot() function you can plot multiple layers on the same map by calling plot() multiple times. You'll need to add the argument add = TRUE to all calls to plot() after the first one and you need to run the code for all layers at once rather than line-by-line.

sf, raster and tmap are loaded in your workspace.

Instructions
100 XP
  • Try to plot the neighborhoods object on the canopy object with plot(canopy) followed by plot(neighborhoods, add = TRUE). Do you see the neighborhoods?
  • Use st_crs() on the neighborhoods layer and crs() on the canopy layer to see if they share a CRS.
  • Save the CRS of the canopy layer as a new object called the_crs. You should use crs() with the asText = TRUE argument.
  • Use st_transform() to transform the neighborhoods CRS to match the canopy layer.
  • Re-run the plot() code from the instruction above. Do you see the neighborhoods now?
  • Simply run the given tmap code.