Tidying Up Tableau Chart Labels With Secret Reference Lines
This post walks through how to create space in your viz without having to fix an axis. There are examples for handling labels both on the edges of both the width and the height of a chart. It is targeted at users slightly beyond beginner who are comfortable with the range of formatting elements in Tableau. There are further possible extensions beyond this, but they are held back for a further future post.
The guidance here amalgamates a few things I’ve learned from participating in Workout Wednesday and watching Andy Kriebel’s Tableau Tip Tuesday series on YouTube. I needed to use this solution to get myself out of a formatting hole recently, and now that I’ve adopted it I find myself doing so regularly.
So often we want to find a way to label values or series on our charts, but Tableau’s defaults can constrain their visibility. This is a particular problem if our data is dynamic and we want to add labels but have limited control over where they might appear. What could have been really clear charts now appear to be cluttered. So, how can we go from this to this?
Line Chart Formatting Fundamentals
Skip over this bit if you are happy with your own approach to building out line charts, but it would be remiss of me to waltz over a whole series of steps I take when building out a line chart having opened a blank workbook in Tableau Desktop. To begin with, this is the basic setup to get going – time measured along the x-axis as a continuous (green) dimension placed in Columns with a measure on the y-axis in Rows. And then a dimension to colour. The example here is taken from Superstore, and the fields chosen simply illustrative:
That will produce the default setup for a line chart in Tableau, as shown below:
I won’t detail everything here step-by-step, but to make this palatable even for me to work with, let alone share with others, I would tend to make the following changes before progressing any further. I lay these out here so that you don’t feel like I’ve cheated on you by jumping to one I prepared earlier without explanation.
Okay, so with all that done, we’ve now got a tidier chart to work from, and are ready to add some labels:
Labelling Each Series
Let’s now add our labels by copying the colour-coded dimension to text, while also matching the mark colour on the font. This is particularly useful when we have multiple series to help distinguish between them when our lines overlap.
By default, Tableau will prevent your labels from overlapping. If we want our chart to live on, whether on Server or picked up in Desktop by a colleague, we might want each series label to appear whatever. Otherwise we might end up with this kind of situation, where two series which end close to each other aren’t labelled at all. Unless we use up space by adding a legend, we can’t tell which category is blue and which is yellow:
But whilst we can force Tableau to show us the labels for other two series, they might overlap with each other, or worse – they might overlap with the data points, like this:
Buying Extra White Space To Play With
So, how can we buy ourselves enough space to add the labels without compromising the ability to read the lines in the chart? With reference lines, and a sneaky calculated field. The key is to add a period after the latest date in the view, using the DATEADD function. In this case I’ve chosen to add 16 weeks, but you can make it days, months or years, and any number. In this case, with some experimenting, 16 worked fine here. The number you’ll need will depend upon the date range on your axis as well as the character length and font size of your labels.
And then we’ll bring that into the view by dropping the field onto ‘Detail’, making sure that it’s set to continuous. And then, add a reference line. The key step in doing this is setting the line format to “None”, so that it doesn’t appear visible in the view:
And some minor tweaks help to tidy things up – aligning the text as required and in this case I added a space character before the label text to help separate it from the line a little.
And so here’s the output:
A Few More Practical Examples
This matters much more when there’s more going on within your view. For instance, in small multiples:
I first really applied this in a scenario not too dissimilar from the one below – trying to label the end of each line with a value was really tough. Here it’s clearly associated with the end of each line and there’s no clutter.
Further Applications – Going Up!
The same trick with reference lines can be applied to the height, too. For instance, in this mocked-up example I wanted to label the mark where the value in the view was at its maximum. The trouble is though that because it overlaps the line it isn’t clear whether it’s 62% or 63%. In a static viz I could move the label around, but if it’s to live on dynamically then we need a better way around this issue.
To remedy that we need to buy some space above the highest mark in the view. So, create a calculated field that finds the maximum point and then adds a factor of 5% (or whatever value is appropriate for your view):
And by adding that field into Detail and creating an invisible reference line, we now have space for the chart to breathe and its label to appear clearly.
The full workbook used to create each of these steps is available to download from Tableau Public.