I've generated a number of plots using the geom_hex and stat_summary_hex functions in ggplot2, and I've adjusted the number of bins until the plot "looked right." That said, I'd like to know the area each bin represents to help interpret the plot.

Below is a simplified example of the set of example data and plot:


    crime <-read.csv("https://opendata.arcgis.com/datasets/6eaf3e9713de44d3aa103622d51053b5_9.csv", stringsAsFactors = F)

    homicides <- crime %>% dplyr::select(long = ï..X, lat = Y, OFFENSE) %>%
      filter(OFFENSE == "HOMICIDE")

     coordinates(homicides) <- ~long + lat
     homicides.df <- data.frame(homicides)

    homicides.hex <- ggplot() + 
      geom_hex(data = homicides.df, aes(x=long, y =lat),
               fun = sum, bins = 20) +

Is there a function or method to measure the area (in square miles or kilometers) of an individual hexagon when the x and y axes are latitude and longitude?

homicides in dc

  • Each row of cells has different area than the row above or below it. – Vince May 27 '17 at 0:42
  • @Vince I'm actually interested in measuring the area of a single hexagon that was generated. I edited the question to clarify my need. – Dave May 27 '17 at 4:54
  • A polygon is a polygon, no matter how many sides. All you need is a geodetic area function. – Vince May 27 '17 at 11:53

Not tried it myself but the GeoSphere package includes a function for calculating area given a polygon defined as lon, lat pairs.

Also got lots of other geodetic and spherical geometry formulae (great circles, vincenty, haversine etc).

An example from the documentation, input units are degrees, output units appear to be square meters

> pol <- rbind(c(-120,-20), c(-80,5), c(0, -20), c(-40,-60), c(-120,-20))
> areaPolygon(pol)
[1] 4.903757e+13
  • This looks promising, but I'm not sure how to extract all of the points of one of the hexagons in order to input the values into the geosphere::areaPolygon() function. Any ideas? – Dave May 30 '17 at 14:50
  • not sure - you might want to look at the source for ggplot2 if you want to find out how the hexes are created in the first place. this blog post covers the maths involved – Steven Kay May 30 '17 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.