Last week I wrote about how to create bounding boxes in Tableau. In my most recent visualization I used them to create a custom zoom control for multiple maps. Click the dashboard to play with the interactive version on Tableau Public.
With the ‘zoom in/out’-parameter on the left side you can vary the size of the bounding boxes and because of the automatic adjusting that tableau does with its maps it looks like you are zooming in and out. If I would fix the maps (see again last week’s post on bounding boxes) a change of the parameter would only change the size of the boxes in the view.
In this post I want to share the steps I went through to build this visualization.
1. Find yourself some data you want to map
I had the idea to look at centers of global capital cities, mainly because it is so easy in Tableau to identify the geographical centers:
- Import a list with country and city in Tableau
- Create a view with generated latitude and longitude
- Export the worksheet as a cross tab to Excel
2. Prepare the data
As described in last week’s post it needs five points to draw a bounding box. Therefore, I quintupled the data and identified each partition as my point order. That’s all data preparation you need (and even this step could have been done in Tableau).
3. Constructing the bounding box
I could then construct my bounding box around my city centers with ∆ lon as width, ∆ lat as height and some basic math:
But then it got tricky. An early version of my viz looked something like this:
All boxes are sized differently. What is going on here?
4. Some spatial basics
I’m not a spatial expert and it took me some time to figure it out. As described in 3. I wanted to add or subtract a lat/lon to my center lat/lon. Unfortunately, a distance expressed in lat/lon is depending on where you are located on the globe (therefore the different sizes of the boxes, the cities are spread around the world).
This is due to the distance between two longitudes. While the distance between two latitudes is (more or less) constant (111.12 km), the distance between to longitudes has its maximum amplitude at the equator (where it is also 111.12 km) and is zero at the poles.
The distance between two longitudes can be calculated like this:
Distance = 1 nautical mile * 60 * cos(Latitude)
Distance at the equator = 1,852 m * 60 * cos(0) = 111,120 m (or 111.12 km) with cos(0)=1
The distance at a latitude of 45 degrees would for example be
1,852 m * 60 * cos(45) = 78,573.7 m
That means: a distance expressed in longitude has different lengths around the globe, depending on the latitude. To kill this effect, I just had to normalize the ∆ lon with the cosine of the latitude which I calculated in Tableau with the following formula:
So far so good.
5. Sizing of the bounding box
In an early version I was now able to create two parameters for height and width (expressed in lat and lon) and to draw the boxes, but then I felt it would be cool to have only one parameter expressing the size of the box (s) in square kilometer. Therefore I had to calculate height (x) and width (y) within Tableau.
To do so it needed the following two equations:
(1) s = x * y
(2) f = y/x with f=format of the box (e. g. 16:9, 4:3 or 1:1)
I could then calculate height and width in meter based on my parameters Square Kilometers, format and lat/lon per 100 m:
resulting in these two calculated fields:
6. Bringing it all together
The last step before creating the view was to calculate the longitudes and latitudes for each partition of my data.
For the latitudes:
For the longitudes:
7. Creating the viz
To create the viz I went through similar steps like described in my previous post.
For the second axis I chose circles from the marks card to place my label with the following formula:
That’s it (+ a little bit of formatting)!
Hope you enjoyed this post and you’ll find own use cases for this technique!