Hexbins & Mapping in Tableau: Tutorial HD with CC June 2016
Let me show you how to bin thousands of data points into neat hexagons, on a scatter plot and on a map.
- Hexbinning requires Tableau version 9 onwards and uses the HEXBINX and HEXBINY functions, which both need to be defined together for the X and Y axes (or lat and long)
- After creating the hexbin calculations, move them from measures to dimensions, then convert them to continuous when placing them on the visualisation
- Use a parameter multiplied into and divided out of the calculation to control bin density on the fly without rebuilding calculations
- For maps, treat latitude and longitude as your X and Y values, and assign the duplicated reference fields the correct geographic roles
- Hexagons are used because they are the most compact way to fit shapes side by side, the same reason honeycombs occur in nature
0:00Hey, Tim here and today I'm going to be
0:02showing you how to create hex spins in
0:05Tableau. I was reading recently a paper
0:09from the Tableau research website on
0:12hex spinning and the paper basically talks
0:14about the distortion that takes
0:16place when you do hex spinning in a
0:19geographical context because of course
0:22the world isn't flat and so when you
0:25project hexagons or any sort of shape
0:28mapping onto a map then actually you're
0:31distorting the information so the paper
0:33covers this is a quite a geeky paper so if
0:35you're into that kind of stuff
0:37definitely head there but it got me
0:39thinking about hex spins in Tableau and
0:41so I wanted to show you today how to do
0:44that for data that might have an X and
0:46Y axis or mapping data so a lat and a long.
0:52The data I'm going to be using
0:53today is actually from this site here it's
0:55just dummy data I've created a
0:58basic schema here so you have customer
1:01details you have the X and Y axis
1:04and then you have latitude and longitude
1:06and just for fun I've added in some
1:08country data in there as well just so we
1:11can very easily distinguish between two
1:14regions. The data I'll be using I'll be
1:17putting that into 9.3.3. Hex spinning
1:21is supported from Tableau version 9 onwards
1:24so that's a really important
1:25requirement if you want to follow along
1:27make sure using version 9 onwards. Okay
1:30so I'm going to connect to my mock data and
1:34as I showed you we have these fields
1:38here so you have some customer data some
1:41country data some emails and then you
1:43have the four fields we're going to be
1:45looking at when we do hex binning. Now
1:48hex binning itself is just like binning in
1:51Tableau so if you wanted to say bin
1:53all your measures you can just right click
1:57on the option go to create and
1:59then select bins and it gives you some
2:02options here about how big those bins
2:06are. Now hex binning is a very similar
2:09concept except for what you're really
2:11doing is you're doing it on a two-
2:13dimensional plane so you're
2:14basically taking items that are near each
2:17other and you're putting them into
2:19a bucket and having one data point to
2:22represent all those sort of neighboring
2:26data points and the function that you use
2:28to do that is called hex bin so when
2:32you type in hex you get these two functions
2:36here hex bin X and hex bin Y
2:39and you need both of them when you create
2:41your hex bin calculations you
2:43need one for the x-axis and one for the y-
2:46axis and if you're doing this for
2:48mapping you need one for the latitude and
2:50one for the longitude so first I'll
2:52start by showing you how to do this for the
2:54x and y-axis so first of all I'm
2:57just going to hit enter to capture the
3:00first hex bin function and then here I'm
3:03just going to type X which is one of my
3:06fields and then Y and that's it we've
3:10created the hex bin X calculation so I'll
3:13give this the value hex bin X
3:15because I'm lazy I'm going to copy this I'm
3:18going to click OK I'm going to
3:20create another calculation I'm going to
3:22paste that in there I'm going to call
3:24this hex bin Y you can see I've just
3:30changed that function there and click
3:33OK so now we have these two new
3:35calculations now step number two the
3:39really important bit is to make sure that
3:41you move these from the measures
3:43pane up to your dimensions pane so I'm just
3:47going to select both of these I'm
3:49going to move them up okay and now they're
3:51there and now we can actually
3:54start to put them into our visualization so
3:56I'm just going to bring out hex bin X
3:58and I'm going to bring out hex bin Y now
4:02immediately this doesn't look like what
4:05I was trying to create I can sort of see
4:07some things happening but it I can't see
4:10any sort of shapes and that's actually
4:12because I've dragged out dimensions onto
4:15the visualization and although we wanted to
4:17do that in the first place when we
4:19visualize them we want it to be done on a
4:22continuous scale so we need to
4:24actually change these to continuous items
4:29okay and as you can see we now get
4:33something that looks fairly close to what
4:35we're trying to achieve you seem to
4:37have data points grouped in a very
4:39organized way you can even see gaps in
4:42the data where there isn't data with
4:44neighboring sort of cohorts and if I
4:48very quickly plot the X and Y axis I can
4:50show you this is actually working
4:53because if I do this and I bring in the
4:56lowest level of detail which I think is
4:59I think it's going to be first name and
5:03custom ID and also those three put that
5:08on detail then that's actually what you get
5:11when you have the full level of
5:13detail so the hex binning is actually
5:16working and so this this is working but
5:20it still doesn't quite look like what we
5:22want to achieve yet there seems to be a
5:24very high density of binning going on so
5:28how do we address that okay well it
5:30turns out we have to edit the calculations
5:34that we created in the
5:35first place and to do that I'm actually
5:37going to create a parameter okay the
5:40parameter will allow us to change the
5:42valley that we're going to use you don't
5:44have to use a parameter but it gives us a
5:46little bit of flexibility and it will
5:48allow me to demonstrate to you how to do
5:50this on the fly so I'm going to call
5:53this parameter density density okay and I'm
5:59going to give it a range I'm going
6:02to give it a range of zero to a maximum of
6:06about ten and I'm going to make the
6:09step sizes quite frequent so I'm going to
6:14do not point zero two and I'm going
6:17to click OK and if I just show the
6:21parameter control you can see the
6:24density is one I'm going to leave that just
6:26there for now and now we need to
6:29adjust our calculation so if I click on
6:33edit and basically what we want to do is
6:35we want to basically multiply these X and Y
6:42values and then divide by a value
6:45so you want to increase the scope the
6:48tablet looks at the data at but then
6:52reduce the values that it doesn't skew the
6:55data probably not explaining that
6:57too well but let me just type the
6:59calculation in and you'll see where I'm
7:01heading so I'm just going to multiply this
7:04by the density I'm going to
7:07multiply Y by the density as well and then
7:12I'm going to divide the whole thing
7:14by density okay and that's hex bin X and we
7:18can actually just copy this part of
7:20the calculation click OK and then paste
7:31that here okay so you've seen me change
7:38these two calculations and yet nothing has
7:41changed now remember the density
7:43value is set to one so if I look at this
7:46value well of course X multiplied by 1 Y
7:50multiplied by 1 divided by 1 is going to
7:53give me the same value as I had before
7:55but the cool thing now is if I change the
7:57density for let's say go down a
8:00little bit you can start to see what's
8:02happening the groups are getting larger
8:05I'm getting less and less circles to the
8:09point where if I was just to take this
8:11down manually to not point zero one two I
8:15have much much much less space going
8:18on there okay so if I just bump up the size
8:22you can see and just as proof that
8:24this is working I'm actually going to bring
8:26in the number of records filled
8:28here and I'm going to put that on label
8:30okay and I'm going to make sure that
8:32this is centered like so so you can see it
8:36inside of the circle and I'm going to
8:39bring the size down a little bit so you can
8:42see what's going on as these get
8:44bigger or smaller let's go on let's make it
8:46a bit bigger actually okay so watch
8:49what happens at the moment we have roughly
8:53350 to 70 items per shape you
8:56can see on the fringes we have less density
9:00and in the middle you have
9:01around 380 in some cases 400 so if I bump
9:06this number up you can see the
9:09number of items in the show squares getting
9:12smaller and if I make that
9:15really small then you can see the text just
9:17about there we're now into the 50s
9:19so this is actually working and this is hex
9:22spinning working on the dashboard
9:24now the last step is actually making these
9:28shapes sort of interesting so in
9:31hex spinning it's actually named after a
9:34hexagon and right now I'm using a circle
9:37now the reason hex bins are used or hex
9:39agons are used is because they're
9:41actually the most compact way of having
9:44shapes side by side if you look at a
9:46honeycomb it's even nature's choice for
9:50having a lot of sort of shapes
9:52compacted into a very small sort of surface
9:55area and you know space so if I
9:59go to shapes here I've actually got my own
10:03hex spinning shapes I comment a bit
10:06crazy with these I actually have quite a
10:08few and I'll be attaching these to the
10:10blog post so if you're watching this on
10:12YouTube hit the link at the bottom in
10:14the comments and it will take you to the
10:16blog post and you can download these
10:17shapes and I'm gonna click apply and so
10:20here we have a very basic hexagon and if
10:24we increase this you can start to see what
10:27's going on and I can even just
10:29maybe bring the width in a little bit bump
10:33up the size and you start to get
10:37that iconic sort of grid that you come to
10:40expect with hexagons where everything
10:42is sort of very nicely and mapping or
10:45fitting side by side and obviously it
10:49looks like we're zooming in it's a bit
10:50psychedelic actually and you don't have
10:53to use a particular shape you can just use
10:55circles and if we make this a
10:58larger as you make this bigger you can
11:03obviously start to see that it just gets
11:06a little bit too busy but it works okay so
11:09we've done one type of hex binning now
11:12I'm gonna do exactly the same thing but for
11:15a map okay so I'm gonna go over to
11:17sheet three actually so this is what it
11:20would look like without binning and this
11:22is what it looks like now now for mapping
11:25we normally use a latitude and
11:28the longitude so let's have a look and see
11:30what that looks like on a map so
11:32let's just bring those two in I never seem
11:35to get these the right way around
11:36and at the moment all our data has an
11:39average around Italy but if we bring in
11:42again the customer level of detail you
11:47start to see we have a very big global
11:50data set now when I created this sample
11:52data I only actually brought in the
11:55dimensions for country if it was France and
11:57the United Kingdom so let's filter
11:59that down and now we've got our data set
12:03just for France and just for England and
12:05here we want to apply some hex binning so
12:08when we do hex binning on a map it's
12:11sort of different because we're still using
12:13the same technique but we need to
12:15actually apply it to some sort of X and Y
12:17axis and of course we don't have that
12:19as we'd expect to see it and that's where
12:21the latitude and longitude come
12:24and you see these are basically the X and Y
12:26values on a map so I'm actually
12:29gonna be a little bit lazy and I'm going to
12:34duplicate these okay and I'm gonna
12:40edit this I'm gonna rename this hex X and I
12:43'm gonna name this map okay and I'm
12:49gonna start with the longitude and latitude
13:00okay click apply and then I'm
13:04gonna do the same for this and as we do
13:13this click apply we now have two fields
13:21so let me just rename this correctly so
13:24that we have a consistent naming okay
13:29perfect okay so now what I'm gonna do is I
13:33'm actually gonna duplicate this so we
13:36have a reference point and in order to
13:40actually make this work we still need to
13:42tell tableau that this is a latitude and
13:44longitude so I'm gonna just change the
13:46data type here and give this a geographical
13:49role so that one latitude
13:51again geographical wire for that one long
13:56itude okay and then I'm gonna clear
13:59this map again I'm gonna bring in my eye
14:05and I'm gonna bring in my X and if I
14:11rotate these a little bit you start to see
14:13okay got some interesting thing
14:15going on here it is binning but first and
14:18foremost I want to bring in just
14:20France and the UK focus it in focus us in a
14:25little bit and the density still
14:29not quite right so what I need to do is two
14:32fold number one I need to bump up the
14:36density okay and you can see very clearly I
14:39've obviously made a mistake
14:40because all the data points are around
14:44Africa and obviously France and the UK
14:46are not around Africa so I've put these the
14:50wrong way round in fact not only
14:53about put them the end right now around I
14:56have also given these the wrong
14:59assignment very easy mistake very easily
15:02fixed okay so now if I put that there
15:07that we have the right lat and long for the
15:13shapes and now when I increase the
15:17density you can see very easily that I'm
15:20now binning within those countries and
15:22just to make it easy to see I'll put
15:24country on color by control dragging
15:27placing that on color and then one thing we
15:31can do is put number of records on
15:34size to show us where is the density of our
15:40customers and we can even go as far
15:43as saying okay now that we've done that let
15:46's adjust the range of this so that
15:49we can see very easily that we do have data
15:53spread across the countries but a
15:56smaller customer bases are in the outer
16:00regions of each country and the bigger
16:04customer bases are obviously centrally
16:07located and you can sort of play around
16:10with this until you get the optimum sort of
16:12distribution and again you can put
16:15instead of country you can put number of
16:17records on color and you can basically
16:20use different techniques here to try and
16:23make your data a little bit clearer and
16:26a little bit easier to decipher I'm just
16:29gonna give this a dark hello there so
16:31you can very easily see what's going on so
16:34that's how we do hex binning on a map
16:37and also an X and Y axis where you might be
16:42using scatter plots let me know what
16:45you think drop some comments in the feed
16:48below I'm gonna attach the workbook that
16:51I've used as is right now so you can
16:53obviously take this apart have a go
16:55yourself and I'd love to hear your feedback
16:57on the video if you've got some
17:00tips let me know and yeah I'll see you soon
A short video highlighting the basics of hex-bining in Tableau on planes and maps. Visit the blog post for the Hexbin shape pack and the Tableau workbook along with the mock data. http://tableautim.com/tableau-hexbin-tutorial-with-hexbin-shape-pack/ Always welcome comments and feedback on the videos and if you have a topic you’d like me to cover add a suggestion in the form.