Using QGIS to create a hex bin map and aggregate data

This is a quick tutorial on how to make a hex grid in QGIS. I am using them to aggregate point data onto uniform grid for display. A lot of the time you will have something like census division or counties to aggregate data to but the problem with these is that the shapes are not uniform. The different sizes really can give false impressions depending on their value and the size of surrounding polygons.

To create hex grids we are going to use the processing plugin. In QGIS go to Plugins>Manage and Install Plugins. In the search dialogue type ‘Processing’, find the plugin and install. After it has been installed make sure the check box to the left of the plugin name is selected.

Using QGIS to create a hex bin map and aggregate data

Once the plugin is loaded a new ‘Processing’ menu will show up at the top of QGIS. Go to Processing>Toolbox. The Processing Toolbox will load on the right of the screen. Expand Scripts>Tools>Get scripts from on-line script collection. Expand the Not Installed list, and check the box by ‘Hex grid from layer bounds’. Now back in Processing under Scripts>Polygons you should see the hex grid tool.

Load up the grid tool and you will see a simple dialogue. It allows you to use an input shapefile for the bounding box and you input the cell size (in the units of the layer projections). The grid can either be generated to a temporary file or saved to a shapefile.

Using QGIS to create a hex bin map and aggregate data

 

 

Using QGIS to create a hex bin map and aggregate data

Here is a shot showing my input shapefile, with the hex grid overlain.

Using QGIS to create a hex bin map and aggregate data

The next step I took is to use a spatial join on the hex grid to the original input shapefile. This will trim all of the hex bins outside of my study area. Go to Vector>Data Management Tools>Join Attributes By Location. In the dialogue the target vector layer is the hex bin grid and the join vector layer is your original shapefile. For attribute summary keep Take attributes of first located feature selected. We aren’t after the data from this join just selecting the study area. Make sure that Only keep matching records is selected so that it will drop all of the hexes outside of your study area.

Using QGIS to create a hex bin map and aggregate data

Now that we have a clean study area repeat the previous process this time with your trimmed hex bin layer and your point data set that you want to aggregate. I have a dataset with sales volume for businesses stored as points. I want to aggregate sales volume so it can be displayed on the hex map. This time we will pick ‘Take summary of intersecting features’ and pick Mean and Sum. Under output table pick ‘Keep all records’ because we want to have a completely filled hex bin, we do not want holes where there are no points. Note that once you are aggregating it is the best if you remove all non numeric fields as they cannot be aggregated (i.e., you cannot take the sum of a name), and this could make the entire aggregation fail.

Below I have created a summary map showing both the total and average sales aggregated to the grid polygons and the dissemination areas. This is a really great example of why having different sized polygons can really throw off your map interpretation. You can see that in the dissemination area map there are very large polygons in the top right of the map. These also coincide with high sales value, so this gives a biased view about the areas with high sales. Compared to the grid map you can see that there are localized areas of high sales, but they do not visually seem as large.

Using QGIS to create a hex bin map and aggregate data

I hope you found this tutorial for aggregating data to hex bins using QGIS helpful. Feel free to contact me if you have any questions.