Ads Top

A Template for Creating Sunbursts in Tableau

In this post, I’ll share a template for creating sunburst charts in Tableau. This template is based on the original work of Bora Beran and a subsequent blog by Toan Hoang, both of which have given their permission for me to use their work in this blog. Of course, it's likely that others have also created sunbursts in Tableau whom I've missed here. If that's the case, my apologies. My goal in writing this blog is simply to help make this chart more accessible to a broader audience of Tableau users. 

Before I jump into sunbursts, however, I want to give a bit of background in case you didn’t read any of my previous posts. If you did read them, then feel free to skip right to the sunburst chart section as this is mostly a repeat of what I’ve already shared.

Templatizing Complex Charts
Unfortunately, some chart types can pretty difficult to create. They tend to include data scaffolding, data densification, unorthodox joins, table calculations, trigonometry, etc. So when I create one of these, I find that it's very helpful to templatize them as much as possible. These templates typically consist of a relatively simple Excel spreadsheet into which some data can be plugged and a Tableau workbook, which simply connects to the spreadsheet. In the end, the process becomes relatively plug-and-play.

While having a plug-and-play template can be valuable, I still want to encourage you to drill into these chart types further. Taking apart complex charts is a really good way to learn some of the more advanced features of Tableau, so I highly recommend that you to take some time to understand how these work under the covers.

Sunburst Charts
A sunburst chart is really just a treemap which uses a radial layout (thus the alternative name, “Radial Treemap”). Sunbursts are a series of rings, which represent the different hierarchical levels. The innermost ring is the first level, followed by the second level which shows a breakdown of the components of the first, and so on. Like the more common rectangular treemap, the size of each sections represents the magnitude of some measure.

The key use case for sunbursts are visualizing hierarchical data. In general, I feel that other types of treemaps, specifically rectangular treemaps and icicle charts (see Adam McCann's blog for more details on icicle charts), are much better for visualizing hierarchies than sunbursts. And, sometimes, a series of bar charts can be even more effective than all of the above. But there are times when sunburst could make sense for your particular use case. Plus, there is definitely something aesthetically pleasing about circles.

Implementation in Tableau
To my knowledge, the first person to implement sunburst charts in Tableau was Bora Beran, in his blog post, Radial Treemaps & Bar Charts in Tableau. In a subsequent blog post called SunburstToan Hoang dug into a Bora's sunburst chart in a bit more detail. I've relied heavily on Toan's blog here, while adding a few additional things to help automate some manual steps, particularly in the data prep side.

The structure of a sunburst chart in Tableau is quite beautiful in its simplicity—each section of the sunburst is its own polygon, which are sized based on proportion of the whole. Since each polygon is drawn in a circular fashion, the calculations used to generate the polygons rely heavily on trigonometry. Building these polygons also requires data scaffolding and some relatively complex table calculations, which I won’t go into here.

The Template
This template includes two components—an Excel spreadsheet and a Tableau workbook, designed to make it as easy as possible to plug in your own data. That being said, it does make certain assumptions. First, it assumes that you have already aggregated your data. Second, it assumes you have a complete hierarchy (i.e. the sum of all values in one level of the hierarchy equals the sum of all values in all other levels of the hierarchy). Adjustments can be made to handle divergent use cases, but I won’t be addressing them here.

Since sunbursts show hierarchical data, it has a sort of recursive nature to it. Data on one record sums up to data on another record. This definitely adds some complexity when building an Excel template and, ultimately, there were many different directions I could have taken. To be totally honest, I’m not sure that the approach I ended up taking is the best, but I’ll let you be the judge of that. 

The Excel spreadsheet (you can find it here) has three sheets: Data, Widths, and Paths

Paths is used to handle the data densification needed to build the polygon arcs, but you need not worry about how it works. You won’t need to modify it—just make sure it’s in your spreadsheet.

The Widths sheet defines the widths of each level of the sunburst. It has three columns:

Level – This is the level of the hierarchy and will join to the Level field in the Data sheet.

Distance – This is the distance from zero where the level will start when you plot the x/y coordinates. In other words, this defines the size of the innermost circle of each level.

Width – This defines the width of each level. Many times, you’ll want the levels to be the same width, but in some cases you may not, so this provides some flexibility.

You’ll want to fiddle with these options until they look the way you want. Generally, you’ll only need to update Distance for the first level then enter the Width of all three levels. The remaining distances will calculate automatically based on the first distance and the widths of each level. Note: The yellow highlights indicates that the value is calculated within Excel.

The Data sheet will be used to populate your pre-aggregated data. It contains just seven columns. You can add more if needed, but these are the seven that are required by the Tableau template. The columns are as follows:

Join – The purpose of this column is simply to join each row in the Paths sheet to each row in Data sheet for the purpose of creating the polygon arcs. But don’t worry too much about this. You simply need to make sure that every row has a value of “link” in this column. Note: Strictly speaking, we could use a join calculation in Tableau to join these sheets together, but for simplicity sake, I often like to include a separate column in my data set.

Level – This column defines the level of the hierarchy (1, 2, 3, etc.).

Component – This column describes the component or element we are visualizing.

Parent – This column includes the hierarchical parent of a component. Except for level 1 components, this must link directly to an existing Component (by definition, level 1 components have no parent).

Lowest Level Value – This column contains the aggregated value to be visualized, but only needs to be populated for the lowest level of your hierarchy. By “lowest,” we are referring to the most detailed level of the hierarchy. So, if your hierarchy has three levels, then you’ll need to populate this column for level 3 only. All others will be calculated automatically by summing based on parents.

Value – This column contains an Excel formula that will sum up the values based on parent. You should not modify the formulas in this column.

Sort – This column defines the order in which the sections will be visualized and is critical to ensure alignment of one level to the next.

To populate the Data tab, enter your aggregated data, for each level, into the Level, Component, Parent, and Lowest Level Value fields. As you enter this data, be sure that you order your records in a consistent manner from one hierarchical level to the next. For example, if your first level contains Office Supplies then Technology, be sure that your second level starts with components whose parent is Office Supplies, then move onto components whose parents is Technology. As you populate the spreadsheet, you’ll notice that Value is automatically calculated. Finally, once all your data is populated and ordered as desired, simply populate the Sort field with 1, 2, 3, 4, etc.

Once you have populated your data, you’ll need to connect it to Tableau. Start by downloading the Sunburst Template workbook from my Tableau Public page. Then edit the data source and connect it to your Excel template. The workbook should update automatically to reflect your data. From here, you can do whatever you like with the chart—change the colors, add filters, update tooltips, etc. just as you normally would. Here’s how it looks with the sample data shown above.

Single Axis Donut
In addition to sunbursts, this method can also be used to create a single-axis donut chart. I recently came across a question on the Tableau Forums which noted a problem with with the most common implementation of a donut which includes a pie chart on one axis and a circle on the second axis, which sits over top the middle of the pie. The forum post noted that, when you hover over or click on a pie slice, you get the following effect. 

A solution to this is to float a blank over top of the donut after you've placed it on a dashboard, but that eliminates the possibility of interactivity. So, one potential solution for this is to use a sunburst. In this case, the sunburst would only have a single level. The data would look something like this.

The resulting donut will not have the same issues as the pie-based donut, as you can see below.
And that’s all there is to it. If you use this template to create your own sunburst, I’d love to see it. But please don’t create one simply because you can. Be sure to closely scrutinize your use case and make sure it’s the right chart type first.

Ken Flerlage, June 19, 2018

No comments:

Powered by Blogger.