# How to measure the area of a hexagonal bin generated using ggplot2?

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:

``````    library(dplyr)
library(ggplot2)
library(rgeos)
library(maptools)
library(rgdal)
library(hexbin)

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) +
coord_equal()
print(homicides.hex)
``````

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? • Each row of cells has different area than the row above or below it. May 27, 2017 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, 2017 at 4:54
• A polygon is a polygon, no matter how many sides. All you need is a geodetic area function. May 27, 2017 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)
 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, 2017 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 May 30, 2017 at 20:38