Our Certified Tableau Trainers are asked a myriad of questions each day. In this series, they share some answers.
One of the exercises we often have in class involves plotting shapes on top of a map and sizing by a specific measure. Instead of using Tableau’s default (blue) circle, we use varying colors and shapes. However, regardless of what shape or color we use in our viz, our size legend always looks something like the one below:
In a recent class, a student named Trent mentioned that having a legend with the appropriate colors and shapes would not only help show relative value, but it would also re-emphasize what measure we were looking at.
“So, how do we actually create such a custom legend in Tableau?”
First, a New Sheet
I worked with Trent to find a solution by creating a new sheet that will serve as our Legend Sheet. In this new sheet, the mark type is Shape, and the color and shape are the same as they are on our map (in this case, a green cross). Next, we will need a calculation that splits out the different relative sizes of the shape.
Creating Size Brackets
In this sheet, we are sizing by total sales in each state, so I used a level of detail calculation to label my size brackets:
{ FIXED [State]: if sum([Sales])<=3000 then "Small" elseif sum([Sales])<=500000 then "Medium" elseif sum([Sales])<=1000000 then "Large" else "XL" end}
I bring this new expression, MAX(LOD Sales), to my Rows. Why use an LOD expression here? Well, if we just brought in SUM(Sales) or MAX(Sales), we would get one label that would have been XL, as Tableau would have aggregated across all states. Since we need a specific number of categories or size brackets, and we are sizing by state, we can create an LOD expression to satisfy exactly what we are looking for.
Sizing Our Marks
Next, I want to size these crosses. I can bring Max(Sales) to Size, but in this case, the differentiation in the Max(Sales) for Medium and Large isn’t distinguishable, so I created one more LOD calculation, ({Fixed [State]:SUM(Sales)}, and then took the max of that value. Now, the crosses are sized by the state that has the most sales within each bracket.
Adding Labels
Lastly, I add a label to show the values represented by each size by bringing that MAX{Fixed [State]: SUM(Sales)} expression to Text as well.
From a formatting perspective, I edit the label to include a >= sign, format my LOD expression to be currency and hide the headers for my initial legends labels (Small, Medium, Large, XL). Bringing that sheet back onto my dashboard, my new legend looks like this:
This works well for this chart, but I should mention that since we are sizing by an LOD calculation, if we filter our data, these legends aren’t going to change with the filter since they are fixed at the state level. If we have a dashboard that we know will have filters, we’ll need to get more creative about how we size and label these shapes.
Thanks for the question, Trent!