Encoding Change in Tableau Line Charts
Jul 31, 2019
Klaus SchulteOk, this is probably not a big thing, but I have been thinking about it for quite a while now: How can I encode change in line charts accurately?
Wait: Encoding change in line charts?
Change is of course already encoded in line charts. For example, when we look at Superstore sales by month we can see the change: when the line goes down, we have a negative change compared to the previous month and when the line goes up, we have a positive change.
Everyone can read a line chart, so again: not a big thing.
So, what is this all about? When you want to highlight certain months now, for example all months with negative change compared to the previous month, and you want to use color for this, then you will get something like this:
For me, change is happening between two marks, the change between January and February for example is happening between 1 and 2.
But in the default, Tableau isn’t able to display this accurately, because the change is calculated in February, and the February mark starts between 1 and 2 and ends between 2 and 3. That’s why you see a lot of red between February and March, although the change between February and March is positive. This does make sense when you know how Tableau works, but this might be confusing to your audience nevertheless.
And we have the same problem for the step line type by the way:
When I was chatting about this with Chris Love, he pointed out, that the common way to deal with this would be to add a second chart to show the change:
Ok, this is a great fall back option, but for this blogpost I’d like to share two ways to encode the change in the line chart itself.
Let’s start with the easier one.
For the step line type, I’m thinking of something like this:
Old:
New:
I’m again highlighting the months with a negative change, but the gradient has gone.
How can this be done:
To highlight my months I’ve built my step line chart manually.
Manually means, that I didn’t use the step line typ, but brought in two marks for each month, the sales from the previous month and the sales from the current month. For the previous sales, I used this formula:
The coloring is happening on a second axis:
I then created a dual axis and that’s it.
You can create something similar, using a jump line on the first axis and the gantt mark for the second axis. But I prefer the line mark for both, because it’s easier to change the size of the lines and to label the “change-lines” at the start or at the end of the line, which is different for negative and positive changes.
For the regular line type, I’m thinking of something like this:
Old:
New:
To create this, it needs quite a hack. I won’t go into all the details here, but will only mention the key steps to create it. I’ve uploaded a workbook on Tableau Public, feel free to reverse engineer it!
The basic idea it to add extra points on the left and on the right side for each month. I therefore tripled the data and calculated a “month position” in a first step:
[placing]
is a parameter to vary the distances between my original and my extra points.
For my three copies of the data, I then calculated variable sales:
When I now set the parameter at for example 0.3, you can see that all the transitions from grey to red and red to grey are happening between my original and my extra points, but never between two of my extra points.
To get my clean line chart, I’m setting the parameter at a very small value of for example 0.000001. The transitions become invisible and I finally have what I want.
As promised: quite a hack, but there might be a few good use cases for this, especially for the step line chart which can be created very easily.
Hope you enjoyed reading and that you’ll find own use cases for this.
COMMENTS