Exercise

Raster data as a heatmap

The predicted house prices in preds are called raster data: you have a variable measured (or in this case predicted) at every location in a regular grid.

Looking at head(preds) in the console, you can see the lat values stepping up in intervals of about 0.002, as lon is constant. After 40 rows, lon increases by about 0.003, as lat runs through the same values. For each lat/lon location, you also have a predicted_price. You'll see later in Chapter 3, that a more useful way to think about (and store) this kind of data is in a matrix.

When data forms a regular grid, one approach to displaying it is as a heatmap. geom_tile() in ggplot2 draws a rectangle that is centered on each location that fills the space between it and the next location, in effect tiling the whole space. By mapping a variable to the fill aesthetic, you end up with a heatmap.

Instructions 1/3

undefined XP
  • 1
    • Create a simple dot plot of the locations in preds by adding a geom_point() layer to the first ggplot() call. Verify that the locations form a regular grid.
  • 2
    • To the second ggplot(), swap geom_point() for geom_tile(), where predicted_price is mapped to fill. Remember that fill is an argument to aes(), which is the first and only argument in your call to geom_tile().
  • 3
    • Create a ggmap() using the corvallis_map_bw map.
      • Add a geom_tile() layer with lon, lat, and predicted_price aesthetics from the second plot.
      • Use preds as the layer's data.
      • Set the layer's alpha transparency to 0.8.