Let's take a look an an example, where we create a hillshade map of Spain, and the elevation of Spain, and plot the latter on top of the former. For those unfamiliar with geography, hillshade refers to shade provided by hills, due to the sun shining from an angle.
An example is provided above. Notice that there's a shadow on the southeast corner of the hill.
One popular format for storing geographical data is the raster format, where each coordinate takes on a certain value. As such, you'd have to load the raster package:
library(raster)(Of course, you would download the package by install.packages('raster') if the above command threw an error message).
Next, download Spanish geographical data:
elevation <- getData("alt", country = "ESP")
Hillshade isn't directly contained within the downloaded dataset. Fortunately, hillshade is some combination of slope and aspect, and the dataset has them both. So let's store slope and aspect data:
x <- terrain(elevation, opt = c("slope", "aspect"), unit = "degrees")
slope <- terrain(elevation, opt = "slope")
aspect <- terrain(elevation, opt = "aspect")
For good measure (this is not really required), let's visualize both slope and aspect:
We let R calculate hillshade based on slope and aspect:
hill <-hillShade(slope, aspect, 40, 270)
Now we reach the essence of what we want to do. Let's plot hillshade, which forms our first layer:
plot(hill, col = grey(0:100/100), legend = FALSE, main = "Spain")
Elevation will form our second layer:
plot(elevation, col = rainbow(25, alpha = 0.35), add = TRUE)
Notice that "add = TRUE" causes layer 2 (elevation) to be plotted on top of layer 1 (hillshade).
Ta-da! You've just completed overlaying maps in R.