In this lesson we are going to learn how to create a contour heatmap of spatial point data as well as create pie plots positioned on the centroid of a polygon.

Contour Heat Maps with ggmap

A contoured heat map is another way to demonstrate data density, especially if you do not have or do not desire to create a heatmap over a shapefile (like the country’s provinces). The contour heatmap uses two-dimentsional kernel density estimation in order to create the contoured heatmap of the spatial point data. Think of this as a histogram in two dimensions

Below we demonstrate how to do this using the crime data

library(ggmap)   ##load the ggmap package so we can access the crime data
data(crime)      ##load the crime data

We will plot the contour map over a ggmap:

houston <- get_map(location = "houston", zoom = 13) ##Get the houston map
houstonMap<-ggmap(houston, extent = "device")       ##Prepare Map

houstonMap +
  stat_density2d(aes(x = lon, y = lat, fill = ..level..,alpha=..level..), bins = 10, geom = "polygon", data = crime) +
  scale_fill_gradient(low = "black", high = "red")+
  ggtitle("Map of Crime Density in Houston")