Contour and Density Layers with ggmap

I am busy working on a project which uses data from the World Wide Lightning Location Network (WWLLN). Specifically, I am trying to reproduce some of the results from

Orville, Richard E, Gary R. Huffines, John Nielsen-Gammon, Renyi Zhang, Brandon Ely, Scott Steiger, Stephen Phillips, Steve Allen, and William Read. 2001. “Enhancement of Cloud-to-Ground Lightning over Houston, Texas.” Geophysical Research Letters 28 (13): 2597–2600.

This is what the data look like:

> head(W)
     lat     lon    dist
1 29.775 -94.649  68.706
2 30.240 -94.270 117.872
3 29.803 -94.418  91.166
4 29.886 -94.342  99.316
5 29.892 -94.085 123.992
6 29.898 -94.071 125.458
> attributes(W)$ndays
[1] 1096

I have already pre-processed the data quite extensively and use the geosphere package to add a column giving the distances from the centre of Houston to each lightning discharge. The ndays attribute indicates the number of days included in the data.

I want to plot the density of lightning on a map of the area around Houston. The first step is to get the map data.

> library(ggmap)
> 
> houston = c(lon = -95.36, lat =  29.76)
> 
> houston.map = get_map(location = houston, zoom = 8, color = "bw")

My initial attempt at creating the map used the following:

> ggmap(houston.map, extent = "panel", maprange=FALSE) +
+   geom_density2d(data = W, aes(x = lon, y = lat)) +
+   stat_density2d(data = W, aes(x = lon, y = lat,  fill = ..level.., alpha = ..level..),
+                  size = 0.01, bins = 16, geom = 'polygon') +
+   scale_fill_gradient(low = "green", high = "red") +
+   scale_alpha(range = c(0.00, 0.25), guide = FALSE) +
+   theme(legend.position = "none", axis.title = element_blank(), text = element_text(size = 12))

And this gave a rather pleasing result. But I was a little uneasy about those contours near the edges: there was no physical reason why they should be running more or less parallel to the boundaries of the plot.

plot-1

It turns out that my suspicions were well founded. After some fiddling around I found that if I changed the extent argument then I got to see the bigger picture.

> ggmap(houston.map, extent = "normal", maprange=FALSE) %+% W + aes(x = lon, y = lat) +
+   geom_density2d() +
+   stat_density2d(aes(fill = ..level.., alpha = ..level..),
+                  size = 0.01, bins = 16, geom = 'polygon') +
+   scale_fill_gradient(low = "green", high = "red") +
+   scale_alpha(range = c(0.00, 0.25), guide = FALSE) +
+   theme(legend.position = "none", axis.title = element_blank(), text = element_text(size = 12))

You will also note here a different syntax for feeding the data into ggplot. The resulting plot shows that in my initial plot the data were being truncated at the boundaries of the plot.

plot-2

Now at least I have more realistic densities and contours. But, of course, I didn't want all of that extra space around the map. Not a problem because we can crop the map once the contour and density layers have been added.

> ggmap(houston.map, extent = "normal", maprange=FALSE) %+% W + aes(x = lon, y = lat) +
+     geom_density2d() +
+     stat_density2d(aes(fill = ..level.., alpha = ..level..),
+                    size = 0.01, bins = 16, geom = 'polygon') +
+     scale_fill_gradient(low = "green", high = "red") +
+     scale_alpha(range = c(0.00, 0.25), guide = FALSE) +
+     coord_map(projection="mercator", 
+               xlim=c(attr(houston.map, "bb")$ll.lon, attr(houston.map, "bb")$ur.lon),
+               ylim=c(attr(houston.map, "bb")$ll.lat, attr(houston.map, "bb")$ur.lat)) +
+     theme(legend.position = "none", axis.title = element_blank(), text = element_text(size = 12))

And this gives the final plot, which I think is very pleasing indeed!

plot-3

11 thoughts on “Contour and Density Layers with ggmap

  1. Douglas Skinner

    Your examples work fine. Perhaps a little more explanation of some of the ggplot "layers" would have helped. Wasn't familiar with %+%W . What is that and where is it explained.

    Reply
    1. Andrew Post author

      Hi Douglas,

      Thanks for the comment. Basically the way that ggplot works is to build up a plot as a series of layers. So you start with the basic plot and then add the contours layer and then the density layer. The %+% syntax is a way to change the underlying data on which the plot is based. This is briefly explained on p. 45 of Wickham, Hadley. 2009. Ggplot2: Elegant Graphics for Data Analysis. Springer.

      Best regards,
      Andrew.

      Reply
  2. beginner

    Can you show me the code to fill "W"? This is the result:

    This is what the data look like:
    > head(W)
    lat lon dist
    1 29.775 -94.649 68.706
    2 30.240 -94.270 117.872
    3 29.803 -94.418 91.166
    4 29.886 -94.342 99.316
    5 29.892 -94.085 123.992
    6 29.898 -94.071 125.458
    > attributes(W)$ndays
    [1] 1096

    THX, the beginner.

    Reply
    1. Andrew Post author

      Sure!

      The raw data files look like this:

      2010/05/11,06:00:03.030465, 36.6889, -86.2840, 17.6, 9
      2010/05/11,06:12:43.310609, 36.4513, -86.1412, 23.6, 10
      2010/05/11,06:12:43.376410, 36.4032, -86.2788, 13.0, 6
      2010/05/11,06:21:45.066010, 36.0815, -86.3496, 12.3, 5
      2010/05/11,06:26:29.411433, 36.1214, -86.3102, 13.1, 6
      2010/05/11,06:26:29.351293, 36.0737, -86.2859, 21.6, 5
      2010/05/11,06:30:36.484562, 36.0858, -86.2168, 22.7, 8
      2010/05/11,06:30:36.508047, 36.0553, -86.3554, 12.1, 5
      2010/05/11,06:30:36.591589, 36.0780, -86.2504, 23.0, 10

      You load these into R using

      W = read.table(f, sep = ",")[,1:4]
      names(W) < - c("date", "time", "lat", "lon")

      I then added in a column for the distance from each of the cities using the distHaversine() function from the geosphere package.

      Hope that helps!

      Best regards,
      Andrew.

      Reply
  3. Pingback: Google Maps and ggmap « Software for Exploratory Data Analysis and Statistical Modelling - Statistical Modelling with R

  4. Pingback: Plotting Lightning Data with R | Lightning Interest Group for Health, Technology & Science

  5. Manuel

    Hi Andrew,
    It really looks nice! Congratulations. I am wondering what was the role of distance in the map you created. You seemed to be interested in this variable, yet there is no indication in the code that you relied on it.
    Thanks!

    Reply
    1. Andrew Post author

      Hi Manuel,

      Thanks for the feedback. The distance did not factor into the map, however, I was interested in the distance of each lightning stroke from the Houston city centre. I used the distance data in another part of my analysis.

      Best regards,
      Andrew.

      Reply
      1. Manuel

        Thank you, Andrew!
        Once again, congratulations. I was just asking because I have been trying to incorporate a weighting variable into a smoothing procedure similar to what you used. So far I have not been successful in achieving it. The "solution" I have been using is to include the smoothing plus a layer that includes the weighted points in the maps (including different shapes and color schemes).

        Thank you again, this is great work!

        Manuel.

        Reply
  6. Ketong

    Hi Andrew,

    I assume that the lighting intensity represented by color. Could you explain which column is the lighting data?

    Thank you,

    - Ketong

    Reply
    1. Andrew Post author

      Hi Ketong, in my post the lightning data is in the data frame W, consisting of the latitude and longitude of each lightning stroke as well as their distance from the nominal centre of Houston. These data were derived from the raw WWLLN data. Does that answer your question? Best regards, Andrew.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>