Level Up: Custom Legends in Tableau

,

Tableau does a pretty good job with its default color legends for charts – little squares of color, labels, the usual – but sometimes you just need something a little more special. In today’s post, the next in the Level Up Series, I’ll show you a couple of use cases where you might want a custom legend. Then we’ll walk through the steps for my favorite method to create your own (+ a bonus method)!

use case 1: Indicate Mark type

For our first use case, let’s take a look at a dual axis chart that uses two different mark types. In the chart below, I’m using both the circle and the line mark type. However, the built-in legend shows both measures as squares.

(Psst – notice the cut off legend text? We’ll get to that in a minute!)

The squares are OK, but the chart would be even easier to read (and more accessible) if the shapes in the legend actually reflected the marks on the chart.

Use Case 2: Combine Shapes and COlors

In the next example, I have a list of help desk tickets, and I want to indicate their status using icons – an orange X for open tickets, a blue ! for pending tickets, and a green check mark for closed tickets. With the default legend, there’s no way to combine both the color and the shape into one legend display: We have to have two separate legends. Not great!

Use Case 3: Better Formatting

We’ve seen this one twice now! With a default legend, you have limited control over the format and display of your text. Longer labels get cut off, and there’s no way to increase (or decrease) the size of the icons or change the spacing between lines.

Use Case 4: Unusual data structures

I wrote about this use case a few weeks ago in my post about advanced color palettes. When you have an unusual data structure on your dashboard, such as using a continuous field to assign colors to non-continuous data, you can end up with a legend that just doesn’t work!

How to Make a custom legend

Alright, enough problems! Let’s talk about the solution. There are a couple ways to make custom legends, but the method I’m going to demonstrate here (using a sheet) is my favorite because it can still be dynamic – if your fields update or are filtered out, the legend adjusts appropriately. At the end of the post I’ll give you a quick overview of a second method (using a text object) that’s more flexible but less dynamic.

Let’s jump in!

Step 1: Create a new sheet

Start by creating a new sheet in your workbook, and giving it a helpful name. Use the same data source as your chart. Then add your legend field (or one of them, if there are multiple) to the text shelf. For this example, I’ll be working with the dual-axis chart from Use Case 1 above, so my legend is based on the “Measure Name” field.

Step 2: Apply The Filters from the chart

Go back to your chart sheet, and select each filter one at a time. Check the “Apply to Worksheets” setting. If it’s currently “All Using Related Data Sources” or “All Using This Data Source”, you don’t need to change anything. If it’s not, click “Selected Worksheets”.

In the menu that opens, add your legend sheet to the list of selected worksheets.

Step 2.5: set up the legend labels

If your legend uses a “normal” dimension, dragging that field to the text shelf will already have given you a list of the values in your legend. If you’re using measure names for your legend like I am, though, we need to do one extra step. Right now, your text field will be showing “No Measure Value”. To fix that, drag the “Measure Values” field from the field list onto the details shelf:

Now we have separate entries for each measure. You may need to edit the “measure names” filter that is auto-generated to include only the measures on your chart, or add extra measures to the “measure values” shelf that aren’t auto added. Go ahead and do that if needed, then move on to the next step!

step 3: set up Shapes

Next, change your mark type to “shape”. This will give us the icons for our legend.

Now we need to add our legend field (mine is “Measure Names”) to a couple of different places. To add the field more than once, you can either drag it from the fields list multiple times, or hold control (or command on a mac) and drag it from another spot on the dashboard to duplicate it.

Add the legend field to “color” and “shape”. You should already have it on the “rows” shelf.

We’re getting there! Let’s make this better.

step 4: Clean up formatting

Let’s fix our formatting. Start by dragging the header bar until you can see the whole label (Click and drag at the arrow:)

Hmm, that looks kind of long. I’d prefer that top label to wrap onto two lines! Drag the header bar back to a narrower width, then right click the header and select “format”. When the menu opens on the left, open “Alignment” and set “Wrap” to “On”. While you’re here, you can also set the horizontal align to “right”.

Then return to the header and click and drag to increase the row height. The text now wraps nicely and is aligned to the right.

This is a good time to make any other formatting changes you would like. I usually remove horizontal borders and switch to a lighter font than the default.

Ok – time to handle the icons! If you want a simple icon that already exists in Tableau (like the X and O in use case 2, or just a square or circle), you can easily use the shape and color menus to choose what shows up on your legend right now. Set the shape and color, adjust the size if you like, and you’re ready to move on to the next step!

In this use case, I want to show a line for one measure and a dot for the other. Since the line shape isn’t included in Tableau’s default shapes, this will take a little more work. We’ll start by adding a custom narrow rectangle shape – you can make your own, or download mine here:

Open your Tableau repository in your file manager, and copy the shape into the “shapes” folder. Go back into the “shapes” menu in Tableau, and select “reload shapes”.

Now select “Custom” from the shape palette dropdown, and you will see the custom rectangle as an option. Assign it to the correct field, and choose an appropriate shape for the other fields – I chose a filled circle.

This is looking pretty good! I want to make one more change here, though – I don’t like how much “heavier” the circle is visually than the line, so I’m going to adjust the sizes. (You can skip this if you’re happy with how they look).

I’ll start by adding the “measure names” field to the “size” shelf.

Now the two sizes are different, but the difference is too dramatic and in the wrong direction. Click the arrow on the size legend and open the edit menu.

Reverse the direction if you need to so that the line is the largest icon. Then drag the upper limit of the slider down, so that the difference between smallest and largest is less.

Click apply, and check how it’s looking. You may have to play around with this to get the sizing just right!

I like the ratio now, but the icons are both too small.

Click on the size card, and drag the slider until the icons are an appropriate size.

Step 5: Add sheet to dashboard

Finally, add your sheet to the dashboard! Adjust the padding and layout and drag the column and row height as needed until it looks just how you want it.

With that, your custom legend is done!

Bonus: A second Method

If you need more customization and control over formatting than the method above can give you, there’s a second option. This method uses a text box, which means that the values are fixed – they won’t dynamically change when your data does – but you have complete control over the layout.

For this method, create a text object on your dashboard. You can copy and paste Unicode symbols into the text object to create the legend icons – this Wikipedia page includes the most common shapes, but googling will find more if you need them – and add colors and formatting within the text editor.

I prefer to use the dynamic option in most use cases, but if I need a very specific format (especially if the text must come after the symbol) or a quick fix, the text box method is a good alternative.

That’s it for custom legends! Enjoy building your own, and make sure to check out the rest of the level up series before you go.

Let’s keep in touch! New tips, tricks, and tutorials are posted about once a week – sign up to get them straight to your inbox!

I don’t spam! Read the privacy policy for more info.

Leave a Reply

Your email address will not be published. Required fields are marked *