Reputation: 1330
A few versions ago Tableau introduced the HexBin
functions to allow geographic information (or, actually, any information grouped by x-y coordinates) to be grouped together in equally-sized hexagonal units. This can be very useful when creating maps where the underlying information isn't evenly spread geographically.
The centre points of hexbins can be plotted directly as point maps but this isn't as satisfactory as actually plotting a map where the hexagons tile the plane. Some bloggers have suggested using shapes instead of points to create something like the desired outcome. This Tableau blog uses hex bins
but plots them using non-hexagonal shapes, for example. This blog suggests the use of custom hexagonal shapes, but they don't tesselate and managing them when the hexbin scale varies (the same scale doesn't work for everything) is annoying.
So is there a good way to automatically create tessellating hexagons to plot on a map that allows different hexagon densities?
Supplementary questions: why didn't Tableau build in this functionality? How to adjust for the shape of the earth when hexagons are based on Lat/lon?
Upvotes: 3
Views: 1418
Reputation: 1330
Yes it can be done in simple ways that work and more complicated ways that correct for the shape of the earth
The hexbin
function in Tableau works on any X-Y data to bin the raw coordinates into hexagonal areas.
On maps, the data usually comes as Lat-Lon coordinates expressed in degrees. Tableau can plot this data directly but usually does this using the somewhat distorting Web Mercator projection (areas far away from the equator are greatly enlarged). This means that hexagonal bins defined on lat-lon will not be equally sized (one degree in an east-west direction represents a much smaller distance on the Earth's surface when the latitude is high and far from the equator but a degree on a north-south line is always about 111km long).
Simple versions ignore this distortion and generate the hexbins from lat-lon coordinates. I'll describe methods based on this simple technique first. I'm basing this on a dataset containing the locations of every postcode in the UK (~2.5m rows with a wide range of density in different geographies).
The basic method involves several steps:
Here is a step-by-step guide.
I define a parameter hexbin size
that represents the radius of the hexbin in degrees. Then the hex bins are defined in the following way based on the Latitude and Longitude values in each row of the dataset:
The hexbiny formula is similar.
The dataset itself contains a field called PointID containing values from 1 to 6. (there are multiple ways to achieve this but doing it in the dataset is easier than doing it in tableau as many databases contain hashing functions that guarantee a random distribution of integers which can be turned into numbers from 1 to 6 by a simple mod function). I define a new field called angle based on the value in PointID (to help define the hexagon's vertices):
Now I can add the points that need to be plotted to define the hexagonal polygons:
The longitude is similar:
At this point the dataset should contain enough rows within each hexbin to guarantee that each hexbin contains rows with every value from 1-6 so all the points of the hexagon are defined.
To plot the hexagons, make the Geographic Role for the plot lon and plot lat fields is set appropriately and double-click each in turn. Then drag the hexbinx and hexbiny to detail and change the chart type from automatic to Polygon. This will give some horrible mess which is fixed by dragging PointID to Path. This should give this (i've also added a count of the number of rows to colour the hexagons and adjusted colours and transparency):
This shows that the basic technique works. But it also shows a key problem with it: the hexagonal polygons are distorted. If you can live with this (it is less of a problem close to the equator or over small areas) thens stick with it.
But if you care about the distortions (the visual as well as the fact that the hexagons don't cover equal areas) then you have to do something more complex. What I describe below is based on work by Sarah Battersby of Tableau.
The adjustments below involve some extra steps over and above the simple solution:
You also need to add a parameter for the radius of the earth which is a conversion factor in the coordinate transformations.
Here is how that works. First the conversion to X-Y coordinates:
Now we can use hexbin grouping on the new coordinates:
The formulae for converting these coordinates back to lat-lon are below (useful if you want to just plot the centres):
Note that it is the latitude formula that is the complex one.
But to make the hexagonal polygons work properly when plotted, you have to add the extra points for the 6 vertices before transforming them back to lat-lon coordinates. The conversion is the same as the formulae above and the final formulae look like this:
The extra part just adds a fixed distance to the coordinate for the hexbin centre based on a hexagon sized by the scaling factor (and there are six of these based on the six values of Angle).
When plotted in the same way as the previous polygon plot it should look like this (when tidied up):
Now both the visual appearance and the actual size of the hexagons are better.
For a Tableau Public workbook containing the above calculations see here.
PS Tableau could and should build most of this functionality into the product as it would dramatically simplify a useful technique.
Upvotes: 2