Binning: An Alternative to Point Maps

May 25 2012 by Nate Smith

Binning is a great alternative technique for visualizing density when working with large data sets. Many of our maps are made with point data, such as locations of banks in South Africa and health facilities in Kenya. While these make for powerful visualizations, sometimes aggregating the data tells a better story or uncovers new findings. One method we’ve been using lately is hexagonal binning, which uses hexagon shapes to create a grid and develop a spatial histogram.

Three examples of hexagonal binning in South Africa, Nebraska, and Kenya

Binning is simple - it’s the number of points that fall within a rectangle or hexagon in a gridded surface. There are other methods to do binning and other ways to interpolate values in between points (if it’s appropriate with your data), but hexagonal binning is an effective way to aggregate and visualize your data.

Getting started with binning

It’s easy to get started making maps using binning. The two free and open source tools I use are QuantumGIS and TileMill. The QGIS community has created a number of plugins that augment the rich features that exist in QGIS. One plugin created by Michael Minn is MMQGIS - this Python plugin is a set of vector tools that do not exist natively in QIGS.

MMQGIS is free and can be installed through the Python Plugin Installer under the Plugins - Fetch Python Plugins menu within QGIS. Search through the list for MMGIS and select Install plugin.

Once you have the plugin installed, you will have a mmqgis menu available under Plugins. We can now pull in our point data, create a grid layer, and aggregate our data. Let’s walk through the steps.

Whether you have a CSV file of data with latitude and longitude or you have an existing shapefile of points, open your data within QGIS. I’ve downloaded Kenyan health facility data from the Open Data Kenya site and opened it in QGIS.

Once your data is loaded, you can open the mmqgis menu and select Create Grid Layer. A new window will open up. This will create a new shapefile layer of a grid - you can choose square, diamond, or hexagon. Depending on the projection and units of your working space, you can set the height and width of your grid cells to correspond to a meaningful distance. MMQGIS distances are based on the center of the grid cells.

Now that you have a new polygon layer alongside the point data, we can use other vector analysis tools available in QGIS. Select Vector - Analysis - Points in Polygon. Choose the input polygon layer and the input point layer, give the new shapefile a filename and click OK. The number of points within each cell will be counted and added as a field within your new shapefile.

Styling in TileMill

Using TileMill we can create a map with our new hexagon grid shapefile. Create a new project within TileMill and add your hexagon grid shapefile as a new layer. You can delete the default #countries layer and the map background and make your Carto stylesheet look like this:

/* Color variables */  
 @r1: #FFFFB2;  
 @r2: #FED976;  
 @r3: #FEB24C;  
 @r4: #FD8D3C;  
 @r5: #F03B20;  
 @r6: #BD0026;  

/* Layers */  
#kenya {  
  line-width:0.5;  
  polygon-opacity:0.8;  
  line-opacity: 0.8;  
  [count=0]{polygon-opacity:0;line-opacity:0;}  
  [count > 0][count <= 1]{polygon-fill:@r3;line-color:darken(@r3,10);}  
  [count > 1][count <= 3]{polygon-fill:@r4;line-color:darken(@r4,10);}  
  [count > 3][count <= 5]{polygon-fill:@r5;line-color:darken(@r5,10);}  
  [count > 5]{polygon-fill:@r6;line-color:darken(@r6,10);}  
}

Change the #kenya text to the name you gave your hexagon layer. You’ll now see the hexagons appear in the preview window. Now you can hit Upload in the Export menu to share you map online in your MapBox account. Log into your MapBox account, create a new map with a custom MapBox Streets layer, and add your new hexagon density layer on top. Now you have a map that you can share in a blog or embed into an application or report.

Checkout the South Africa map, Kenya map, and Nebraska map in full screen!