Visualising data with a hexagonal heatmap in Swift Charts

When creating charts of spatial data, readings are commonly aggregated into approximately equal-area cells to form a heatmap. In this post, we will use Swift Charts to plot earthquakes across Aotearoa New Zealand with hexagonal cells.

A hexagonal grid is not only visually striking, but can also approximate the contours of land and political borders more naturally than a rectangular grid. Here, earthquake frequency determines each cell’s colour on a symmetric logarithmic scale.

## # Arranging points in a hexagonal grid

A typical heatmap uses rectangle marks arranged in rows and columns. Hexagonal cells form staggered rows, which we can describe using axial coordinates to identify each cell with two integers, q and r. A point at each calculated centre can then establish our grid.