How To Add Contextual Backgrounds To Scatter Plots In Tableau
Earlier this week I created and shared a viz that used a background image to give context to the points on a scatter plot I’d produced. A few people subsequently asked about the process, so I’m going to lay it out in detail here. This post will discuss the design choice that led to creating such a view, the process of creating the background image itself, incorporating it into a scatter plot in Tableau, and associated tips and challenges in using this technique. I’ll also reference a few useful resources that you might find useful if you want to create your own version.
This visualisation took three forms as I built it. First, I created a static version without any labels, annotations or interactivity. Second I then took a duplicated version of this and added an annotation layer, which is the version you see below and which I circulated online, still static. And third was another duplicated version of the original, but this time with the interactivity added. The interactive version is accessible on, and downloadable from, Tableau Public, here
The Design Process
Rather than trying to squeeze a dataset into a chart type – something we can all be guilty of sometimes – I was delighted that this presented itself as a response to a question. This came up in an office discussion of the recent Wimbledon championships, which Novak Djokovic won, extending the run of grand slam wins by the Federer-Nadal-Djokovic triumvirate to 11 competitions. It also marked three years since the last 20-something player won a men’s major title. We wondered when the last time it happened was, and presumed that this meant that no player born in the 1990s had yet won a tournament.
I quickly scribbled a dotplot on a piece of paper, with each tournament win represented by a dot – 4 per year over time. This would surely demonstrate that there was a gradual ageing of the recent winners.
In sketching this out, it evoked memories of an excellent and often-cited chart created by Rody Zakovich from around a year prior, albeit with basketball data. This is shown below, and the interactive version can be viewed and downloaded from Tableau Public. Rody’s work was emulated by others too, notably Steve Fenn with MLS data.
Additionally, Rody’s viz was also used as one of the challenges in Workout Wednesday, so if you want to try and recreate his viz from scratch you can obtain the data set and instructions here. And while you’re there, take a look around the Workout Wednesday website overall and get involved if you want to find a fun way to develop your Tableau skills.
Creating The Diagonal Background Image
The method for creating the background image is not as impressive or as responsive as you might imagine. It is, itself, a series of polygons plotted onto a pair of axes. If you haven’t created a polygon before, all that you need to know for the purposes of this exercise is that it is a series of coordinates plotted with values such that, when joined up, they create a shape. And in this case that shape is going to be a trapezium.
I created this dataset in Excel, manufactured by looking at my scatter plot and doing some basic maths. I worked out the desired minimum and maximum values of both axes (15-37 and 1975-2021), and for each birth decade calculated the first and last year (x-axis) that someone aged 15 could be and that someone aged 37 could be (y-axis). So, four records for each decade and, crucially, they are assigned an identifier. These are important to remember, as Tableau needs these to literally join the dots.
So now this is our second datasource, and once connected to it we can create a view. It doesn’t need to join on to the existing data, it should standalone. The below screenshot illustrates the required setup – put the relevant continuous (green) fields onto rows and columns, bring the discrete (blue) fields that identify each dot (in this case ‘Position’ and ‘Decade’) into detail, and then switch the mark type to ‘Polygon’. Then switch the Position field onto the ‘Path’ mark, which create the dot-to-dot, and use the field that should distinguish the polygons from one another to ‘Colour’. Then select your colour ramp.
To enhance my view, I added custom annotations for the ‘Born In The 80s’ style sections. Note that you cannot add labels to polygons (yet) without further, relatively more complex, work.
Finally, for this stage, export the worksheet image in the usual fashion, being sure that the only box you tick is the one named ‘View’, and not the Title or the Legend. Save that somewhere that you’ll be able to access it again shortly.
Adding The Background Image To The Scatter Plot
Now we can return to our scatter plot and lay the image we’ve just created as the backcloth to our view. This will give the marks in the chart further context than just the two axes, plus any other characteristics you’ve assigned such as colour, size or label. The menu option is an unusual one, tucked away under ‘Map’. Click ‘Map’ and then ‘Background Image’. This will expand out to a list of datasources you have in your workbook. Be sure to select the one which has got the data from your scatter plot included – you’ll need to reference these shortly. Click ‘Add image’.
You’ll be presented with the dialog box above, and you will want to populate the various sections as demonstrated below.
Name: this can be anything, but make it meaningful, particularly if you have many of them, or versions of it
File: select this by browsing for the image we exported in the last stage
X-field: Select from the dropdown the field that is on the x-axis (horizontal)
Y-field: Select the field from the y=axis (vertical)
The boxes for ‘Left’, ‘Right’, ‘Bottom’ and ‘Top’ should be the minimum and maximum values on your axis (and it’s probably going to be best here if your axes are fixed).
Once you have done this, your background should have fallen into place. If you need to adjust anything in the original, just cycle through the same process by editing the polygons (positions or colours), exporting the image and then fiddling with any of the positions in the dialog box.
Things To Watch Out For
Once you’ve got your view showing the scatter plot laid over the background image, you’ll probably want to feature it within a dashboard. Be very conscious that this could upset the layout of the image versus plot, usually creating a blank space either above and below or either side of your background as the chart axes are forced to scale around your image. To rectify this you may need to fiddle with the dimensions of the sheet object in your dashboard, perhaps even to the exact pixel. This can be a source of frustration, but it is worthwhile.
This is going to work best with continuous (green) fields that are set to a ‘Number’ field type, and not dates. And it definitely won’t work with discrete (blue) fields – they don’t create axes.
Other Applications Of This Technique
I had previously used this approach to solve a slightly different problem. I wanted to show a line chart and then a point on that line within a tooltip, to show the point in time that a mark in a chart represented. Here’s the Tableau Public viz, and the image below shows what the tooltip looked like. In this case the line is a static image and each red dot is a single coordinate on the plot. As you move around the chart, the position of the dot changes. This was necessary as I needed to filter within the ‘Viz In Tooltip’ by date, and if the line was plotted on a dual axis this wouldn’t have shown.
Another, more fun application, which could have practical extensions, is this viz by the genius that is Gwilym Lockwood.
If you’re interested in applying this technique to your own data, I’d recommend starting with Rody’s Workout Wednesday exercise, or even replicating mine as described. Any comments, suggestions or problems, let me know!