User Toggle for Consistent or Independent Axes: A solution for stacked bar charts

     


I really don't like stacked bar charts - they have so many inherent problems. But sometimes, they are, in fact, the best chart for the job. But when I design a stacked bar chart, I take numerous steps to make the chart the most insightful and easy to understand as possible. I've spoken about many of these techniques in past blog post and presentations and I will refer to those today, plus I'll add in one technique that I think takes it to the next level - allowing a user to toggle between consistent axes and independent axes. I'll explain more about this a bit later. 


So let's assume you are starting off with this chart (you can follow along in my workbook published here on Tableau Public.





This chart shows the sales for three different sub-categories - binders, labels, and storage using the Superstore data set. As many have discussed previously (including me), the problem with stacked bar charts is that you can only properly make comparisons for the slice that aligns with the axis - in this case storage in purple. Since they have the same starting point, the bars are easy to compare and allow us to easily compare the length of the bars - what a bar chart does so incredibly well.  But 
you cannot make easy comparisons for the green and orange slices (mmm now I'm thinking about those delectable little orange slice gummies you get from Cracker Barrel) because they start at different points. For example, which is larger Binders in 2022 or 2020...and how much larger?


My first step in resolving this is to do two things: unstack them then add a stacked grand total. This isn't super straightforward, but I discussed this trick in volume 4 of my ten tips and tricks as well as in our TC23 presentation. If you follow those steps, the above chart will first turn into the following:




This is much better (in my opinion), but still has some issues. Let's first talk about why it's better. I can now see each slice (each sub-category) individually with all of their bars starting at the same point. To use the example above, now I can easily tell that Binders in 2022 is larger than Binders in 2020 and by approximately $7000 (just an estimate).  In addition, I can see them all together in the stacked bar grand total. This grand total gives me all the information I had in the original chart. So we've definitely improved it.


I think the problem with this chart, however, is pretty glaring. It's the orange Labels sub-category. The slice is so small compared to the other sub-categories and when plotted on the same consistent axes as the other two sub-categories (up to about $150,000), the values can be hardly seen, let alone give you any idea of trending within that sub-category.





So the way that I handle this situation is to provide the user with the option to show these "slices" on either consistent axes (like we do above where each axis runs to approximately $150,000) or on independent axes (where each slice would be on it's own independent axis).  An example of both are shown below for the sake of comparison.





I noted the problem with the consistent axes above, but there are also issues with independent axes. I think the major issue is that the bars for Labels appear to be the same height as the bars for Blinders and Storage - when we know they are significantly smaller. I will address this as I show you how to give your users the ability to switch between Consistent Axes and Independent Axes. So let's build it.


In the end, our goal will be to feed in a reference line that aligns our charts to have consistent axes but allow users to switch to independent axes if desired. 


First, create a string parameter with two options, one for Consistent Axes and another for Independent Axes (make sure you include instructions somewhere as to what the differences to ensure the average end-users has full understanding).  


Next, set your axes to Independent as shown below:





Now let's think about our goal. When the parameter is set to Independent Axes, well...we need to do nothing. As mentioned in the last step, the axes are already set to independent. But when the parameter is set to Consistent Axes, we need them all to align. We can't change the options in the Edit Axis menu (above image) using a parameter, but what we can do is feed in a reference line. If we feed in a reference line of the max value in our view, then we will align all of the axes to that max value. Before we write any calculations, let's just try it out: 





So as you can see, all we need is to use the Sum of Sales and set it to Max in our reference line. But remember, we need that value to change when we change the parameter.  So let's create a calculation to do this:


Axes Type
IF [Change Axes Layout] = 'Consistent Axes' THEN [Sales]
ELSE NULL
END


So when Consistent Axes is chose, we will use that Sum of Sales values we just discussed and when Independent Axes is selected, it will yield a null value. Now let's add this calculation to the detail card with an aggregation of Sum. Next, add a reference line using the SUM(Axes Type) field, set it to Entire Table, Maximum, don't show any labels or tootlips, don't show any lines, and uncheck the recalc box at the bottom as shown below:





Now, let's give it a try.




It works!


So there are a few other things that I think are really helpful when providing this ability to your users. First, always default to Consistent Axes. Your users should see the actual values in context with each other as a starting point. If they want to change it to Independent Axes in order to see trends, that's great, but it should be an intentional step they take with an understanding of what they are doing. Second, I suggest feeding the parameter value into the subtitle of the chart to always show how it is being displayed. Something like this:





Well, that's it. This is a technique that I use all the time. I think it takes a stacked bar chart and truly takes it to the next level. I'd love to know that you think. Again, you can check out the workbook on Tableau Public here.





Need help with anything related to Tableau?  Through Moxy Analytics, Ken and I provide consulting services such as Tableau Lifeline (get us for 1 hour to help solve a sticky problem), Fractional Data Hero (get us on your team for N number of hours a month for whatever you want us for), Tableau Training, and of course, project work. Click the Icon below if you are interested.




Kevin Flerlage, July 8, 2024




2 comments:

  1. I find using the stacked area chart in place of the grand total stacked bar. The stacked area does a good job of showing relative change each x period. For instance, I built a chart to show all of the ways customers create cases for our support teams and recently we added in a AI chat bot and my dashboard customers are able to see the growth over time of the AI chat bot and the related drop in support cases easily.

    ReplyDelete
  2. Very clever

    ReplyDelete

Powered by Blogger.