# Generating Grid Maps from shapefiles

I've become a big fan of a visualisation technique called grid maps. Here are some examples:

There is a tutorial on automating the creation of these using D3.js and force directed layouts but I'm not skilled in this area (plus I'd feel more comfortable handling a shapefile than I would SVG, even if it is grossly distorted in terms of geographical accuracy):

Are there any QGIS / R / Python techniques which could be used to convert a shapefile to a grid map based on evenly sized squares or hexagons (whilst retaining each feature's data)?

I realise that this would completely destroy/distort the geographic accuracy of the shapefile, my interest is being able to generate these for use as high level visualisations using ggplot in R.

Edit 1: With a single tag edit, iant opened my eyes to googling for 'cartogram' (d'oh) which has yielded this site's consideration on the issue: https://richardbrath.wordpress.com/2015/10/15/equal-area-cartograms-and-multivariate-labels/

Also, I thought I had duplicated another post but their question is a bit different as they want an equal number of a measure to fall within each feature: How to create contiguous area cartograms in QGIS or R?.

Edit 2: In response to stev-k's answer, here are the results from running the suggested script. Red points are centroids of the polygons (in grey) with the hexagons overlayed to show how some are empty and some are crowded.

## 3 Answers

I suspect that your challenge is not with generating the grid but rather deciding how to assign the pre-existing geographies to particular hexagons in the grid (assuming n.polygons == n.hexagons). One approach might be to use the Hungarian Algorithm. Given the distances from the centroid of every pre-existing geography to the centroid of every hexagon in your grid (or some other distance) the Hungarian algorithm could calculate the solution that minimises the sum of displacements from the original locations to the new locations. For more information have a look at an early stage R package which does this here.

Of course, this approach does not guarantee that polygons will maintain the same neighbours but might go some way to helping. I am still trying to solve a graphical approach to this and have a question out here which addresses this.

You can do it in QGIS. Firstly enable the Processing plugin (if you haven't already). Then in the Processing panel, go to Scripts and expand. Then go to `Get Scripts from Online Collection`. There, select the `Hex Grid from Layer Bounds` and install it. Then use that tool with Processing->Scripts->Polygon->Hex Grid from Layer Bounds.

That will create your hex grid. Then you need to spatial join your points (assuming they are points) onto your grid to enable them to be shown. There is no spatial distortion, just spatial aggregation - I wouldn't call that distortion - it's a standard technique in geographic visualisation and processing.

• The problem here is that I immediately have a large number of hexagons which contain multiple points (based on the centroid of my polygons). However if I reduce the size of the hexagons to prevent this then I will end up with an extremely 'gappy' map. Commented Oct 6, 2016 at 14:40
• I'm not sure I understand. It doesn't matter if you have multiple points in a single hexagon - that is the point as this technique is generally used for showing density more clearly through aggregating points or similar into a choropleth map. If you want a cartogram this is going to be based on the shape of the original feature, not a hexagon or other grid. I would also look at voroni polygons. Commented Oct 6, 2016 at 14:49
• What I'm after is for each and every feature (UK councils in this case) to be represented by a single hexagon or square. Each hexagon/square should be the same size and not overlap. The fact that the original polygons are a mixture of big and small means that some movement is inevitable. The difficulty (from my perspective) is finding an algorithm which will 'jostle' the resulting hexagons/squares so that they sit roughly in the right location/neighbourhood whilst allowing for the changes in size. Commented Oct 6, 2016 at 14:57
• Here's some additional info on the method: blog.apps.npr.org/2015/05/11/hex-tile-maps.html Commented Oct 6, 2016 at 15:07
• OK, I see what your are trying to do. There is unlikely to be an algorithm given that a significant step in the process is manual, and this looks like a relatively new area. I don't think there are are any tools outside of D3 that could do this but I could be wrong. Any investment you make in D3 and Javascript will pay dividends though! Commented Oct 6, 2016 at 16:53

Retrospective answer to my question, an R package has been released which addresses this very issue: https://cran.r-project.org/web/packages/geogrid/index.html

More info here: https://github.com/jbaileyh/geogrid