How to Build a Treemap in Tableau with Selected Levels of Detail
Earlier this month I wrote a blog post about Using Treemaps to Visualize Data
where I explain how and when a treemap can be useful in data visualization. Last week Andy Kriebel
posted a Workout Wednesday challenge to Highlight a Treemap
, which demonstrates three different techniques to highlight a segment in a treemap. This post will demonstrate how you can change the level of detail that the treemap uses and even use different levels of detail within the nodes of the treemap.
Building a treemap
Like my friend Andy, I rarely use the Show Me in Tableau. I demonstrate it in training sessions, but I don't find it useful day-to-day. Andy used the Show Me to create his treemap for his blog post, so let's start by building one without the Show Me.
The treemap is an odd chart to create in Tableau. Why? Because it does not have a pill on the Rows or Columns. Everything is done on the Marks Card. Even so, the chart is very simple to build. For this demonstration I will be using the World Indicators data set that is part of Tableau Desktop.
Build a treemap:
Open Tableau and select the "World Indicators" data
Move Region to Color
Move Country to Details
Move Population Total to Size and change the Measure to SUM
Add Year to Filters and select 2012 (this will filter the data to a single year)
You now have a basic treemap. You can move Country to Label if you would like to have the countries listed in the treemap and you can add other fields to the Label, for example, I added the Population Total.
That was easy! It's important to understand what is happening though. Whatever level of detail is in the marks card is the level that will split the treemap. If Region
(color) and Country
(Details or Label) are both on the Marks Card then the treemap will show both levels in the treemap using that hierachy. Whatever field is on top will be the primary split and so on. If Country
is removed then the treemap will only show Regions without a split on Country. Now we'll add some functionality to it so we can choose various levels of detail.
Building the Level of Detail
The first step to create different levels in the treemap is to create a parameter.
Step 1: Create a new parameter
Parameter Name: Select Region
Data Type: String
Allowable Values: List
Select Add from Field
and select Region
Once the values for the Regions are filled in, add All
to the list.
Right-click on the new parameter and select Show Parameter.
Step 2: Create Calculated Field
Calculated Field Name: Detail Level
if [Select Region] = [Region] then [Country]
elseif [Select Region] = 'All' then [Country]
Step 3: Use the New field Detail Level
in place of Country
This new pill will now be used in place of Country. Just drag the new field on top of the Country pill. When a Region is selected from the new parameter, the level of detail will now be selected. Let's examine what the calculation is doing.
If All is selected then it will be at the lowest level of detail - Country.
If None is selected then it will be at the highest level of detail - Region.
If any of the Regions are individually selected then it will use the Country for that selection and Region for everything else.
This parameter now allows the user to select different levels in the treemap to create different comparisons. For example, when selecting "Asia" from the list, it's easy to compare the Country
of India to the Region
of Africa, which is now displayed as a single Region instead of Countries. In addition, putting the Sum of the Total Population on the Label will sum the Population at the different levels being aggregated (Region and Country). Click the image below to enlarge.
This approach offers additional comparisons for the user. As I outlined in my earlier blog post about Using Treemaps to Visualize Data
, when there are small numbers of categories, ex. there are only six Regions, a bar chart is almost always better. However, as research has shown, a treemap offers better accuracy for leaf to non-leaf comparisons. By adding this functionality, the user can now quickly create their own leaf to non-leaf comparison at the desired level of detail. You can download an example Tableau workbook here
I hope you find this information helpful. If you have any questions feel free to email me at Jeff@DataPlusScience.com
Jeffrey A. Shaffer
Follow on Twitter @HighVizAbility