Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in BI by (47.2k points)

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 center 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?

1 Answer

0 votes
by (17.6k points)
  • Yes it can be done in simple ways that work and more complicated ways that correct for the shape of the earth

  • In Tableau, hexbin function 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. Let's describe the 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:

    • Using a parameter that allows adjustment of the size of the hexagons,Generate a scaling factor

    • In order to define the extra points for the vertices of the hexagon, generate value  I have got this by adding a value to each row on the dataset that consists of a random number from 1 to 6

    • Generate the lat-lon coordinates for the hexbin centres

    • Now, add the 6 points that define the vertices for the hexagons

    • On a map, plot the hexagons as polygons (they can then be coloured by any aggregate based on the underlying data)

  • Here is a step-by-step guide.

  • Lets 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:

hexbinx formula

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):

angle formula

  • Now I can add the points that need to be plotted to define the hexagonal polygons:

plot lat formula

  • The longitude is similar:

plot lon formula

  • Within each hexbin,the dataset should contain enough rows 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. It must result in this way (I've also added a count of the number of rows to colour the hexagons and adjusted colours and transparency):

simple hexbin map

  • This shows that the basic technique works. But still it 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) then 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 ever i have described below is based on work by Sarah Battersby of Tableau.

  • The adjustments which are done below involve some extra steps over and above the simple solution:

  • Generate a new scaling factor based on distance

  • Convert the lat-lon values into X-Y coordinates in the Web Mercator system based on distance not lat-long degrees

  • Generate the hexbins using distance not angle in the new coordinate system

  • You need to convert the distance-based hexbins back to the lat-lon coordinates to define the hexbin centres

  • You need to add the hexagon vertices using the distance coordinates and then convert back to lat-lon

  • 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:

mercator x formula mercator y formula

  • At this point of time, we can use hexbin grouping on the new coordinates:

hexbin mercator formula

  • The formulae for converting these coordinates back to lat-lon are below (useful if you want to just plot the centres):

adjusted hexbin lat adjusted hexbin lon

  • Make sure that it is the latitude formula that is the complex one.

  • But in order 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:

plot lat adjusted plot lon adjusted

  • Additionally, 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).

  •  It should look like this (when tidied up), when plotted in the same way as the previous polygon plot:

adjusted polygon map

  • Now both the actual size of the hexagons and visual appearance are better.

  • see here for a Tableau Public workbook containing the above calculations

  • Tableau must be able to build most of this functionality into the product as it would dramatically simplify a useful technique.

Browse Categories

...