11/22/2020
Layering Mark Types in Tableau 2020.4
One of the new features in Tableau 2020.4 (currently in pre-release, but probably coming soon since Tableau Public is already running it) is the ability to layer multiple shapefiles on top of each other. This will be super handy for maps, but we can also leverage this feature to layer multiple mark types on the canvas outside of maps. One popular method to layer mark types is by using a dual axis. However, we are limited to two marks in that case. In this blog post I will outline how you can use this feature to layer marks for more than just maps.
To do this, we will leverage the generated latitude and longitude. And to do that we will use the MAKEPOINT function in Tableau. Instead of using a true latitude and longitude, we will substitute those with two measures.
So here’s the trick. Typically longitude is placed on the columns and latitude is placed on the rows. If you reverse this layout, Tableau plots a scatter plot. You can try this out with the Superstore data. Double-click City and Tableau will plot a map. Then click the little icon on the top to Swap the Rows and Columns (or CTRL-W) and Tableau will change the map to a scatter plot.
For this demonstration, I am going to use the World Indicator data set in Tableau. You will need to put your measures in a MAKEPOINT function. However, in the MAKEPOINT calculation, you will put the measure that you want on the columns as the Latitude and the measure that you want on the rows as the Longitude. This is backwards to our normal thinking, but this is because when we are done placing the pilss we are going to swap the fields (changing it from a map to a scatter plot). So, we build the map, add the layers that we need and swap the rows and columns.
Let’s give it a go.
Step 1: Creating Calculated Fields for Measures
The first step is creating a calculated field based on the mesaures that you want to use. In this viz, I’m going to plot circles for a specific year, but show a line for a 10 year trend. To do this, I will only plot the measures for the circles when the year = 2010.
The two measures that I’m plotting are Birth Rate as Latitude and Life Expectancy as Longitude (the MAKEPOINT will actually plot coordinate systems too, but that doesn’t matter here). We will also duplicate this calculated field for the Density mark.
Calculated Field: Circles
Formula: IF DATEPART(‘year’, [Year]) = 2010 THEN MAKEPOINT([Birth Rate], [Life Expectancy Female]) END
Calculated Field: Density
Formula: IF DATEPART(‘year’, [Year]) = 2010 THEN MAKEPOINT([Birth Rate], [Life Expectancy Female]) END
Next, create a parameter for Country Selection. This will allow us to use a Parameter Action to hover over a country and show the 10 year trend.
Parameter Name: Country Selection
Data Type: String
Allowable Values: List
Add values from: Country/Region
Calculated Field: Line for Country Selection
Formula: IF [Country/Region] = [Country Selection] and DATEPART(‘year’, [Year]) <=2010 THEN MAKEPOINT([Birth Rate], [Life Expectancy Female]) END
We now have three mark types that we will layer on the Tableau canvas in a scatter plot. Notice that the pill types for these three calculated fields is a Geometry.
Step 2: Building the Viz
Double-click Circles to create a map. Longitude (generated) will go to the Columns and Latitude (generated) will go to the Rows and Circles will go to Details on the Marks Card as a COLLECT (plotting a map).
This next step is important. Drag the new calculated field Density over on to the canvas, but before you drop it, you will see a Add a Marks Layer in the top left hand corner of the map. Drop the field in that box to add a new layer. A second Marks Card is created.
Repeat this step for the Line for Country Selection field. Drag it on to the canvas and drop it in the Add a Marks Layer box in the top left hand corner of the map. Another Marks Card is created. You can repeat this as needed to create additional layers.
Once you have the all the layers that you need, simply click on the little icon on the top bar to Swap Rows and Columns (or simply hit CTRL-W). You now have a scatter plot with three marks labels. Each Marks Card can be configured and formatted as needed.
On the Circles Marks Card:
Change the dropdown box to Circles.
Drag Country/Region to the Details on the Marks Card.
Drag Population Total to the Size on the Marks Card.
Click Color on the Marks Card and change the color to a light gray, with 40% Opacity and a black Border color.
Adjust the Size slider to make the circles bigger.
On the Density Marks Card:
Change the dropdown box to Density.
Drag Country/Region to the Details on the Marks Card.
Right-click and Drag CO2 Emmissions to Color on the Marks Card and select Average as the aggregation.
Click Color on the Marks Card and change the color to Density Gray-Red Light, with 93% Intensity and 100% Opacity.
Adjust the Size slider to make the circles bigger.
Select the down arrow on the right of the Density Marks Card name (the line above the dropdown option box) and select “Move Down”. This option allows you to move the mark up or down, changing the layering order, which is also known as the Z-Order. See more information on Z-Order in this blog post Z-Order in Tableau.
On the Line for Country Marks Card:
Change the dropdown box to Line.
Drag Country/Region to the Details on the Marks Card.
Right-click on the NULL indicator in the bottom right hand corner and select Hide Indicator.
Drag Year to Path on the Marks Card.
Right-Click and drag Year to Size on the Marks Card and select the first YEAR(Year) option for the date format.
Right-Click and drag Year to Label on the Marks Card and select the first YEAR(Year) option for the date format.
Click Label on the Marks Card and Select Line Ends to only show the labels for the first and last year.
You now have a scatter plot formatted with three different Mark types.
Step 3: Adding Parameter Action
Let’s add a Parameter Action to Highlight a Country and add an additional mark layer for the country label.
Select “Worksheet” from the top menu bar and select “Actions…”.
Click “Add Action” and select “Change Parameter…”
Select “Run Action on:” Hover
In the Parameter dropdown select Country Selection
In the Field dropdown select Country/Region
Click OK and Ok.
Now when you hover over the countries, the target country should have a 10 year trend line showing.
Step 3: Adding Additional Layers
Adding aadditional marks is fairly straight-forward. Create the calculated fields that your need. Then simply swap the Rows and Columns fields again (CTRL-W), add your additional mark and swap it back.
Let’s add an additional mark for the country label as a demonstration. First we need to create two more calculated fields. The first one will populate the Country name when it matches the parameter, which will updated based on the Hover action. Second, a new MAKEPOINT calculation to hardcode the position of the label on the canvas.
Calculated Field: Country Label
Formula: IF [Country Selection] = [Country/Region] THEN [Country/Region] END
Calculated Field: Country Label Position
Formula: MAKEPOINT(.025,50)
Click on the little icon on the top bar to Swap Rows and Columns (or simply hit CTRL-W) to swap it to a map view.
Drag the new Country Label Position field on to the canvas and drop it in the Add a Marks Layer box in the top left hand corner of the map. Another Marks Card is created.
Again, click on the little icon on the top bar to Swap Rows and Columns (or simply hit CTRL-W) to swap it back to a scatter plot.
On the new Country Label Position Marks Card:
Drag Country/Region to Label on the Marks Card.
Change the dropdown box to Text
Select Size and drag the slider to make the text bigger.
Select Text to format the text as needed.
If you need to add additional marks, just repeat these steps. Create a calculated field to plot the meassure or a fixed position. Swap the Rows and Columns to change to a map view, add the additional layer and swap the Rows and Columns back to make it a scatter plot. Then format the new Marks Card as needed.
Caveat: Luke Stanke pointed out that the generated latitude and longitude are bounded at 180 and -180. In this case, I was within that range, so it worked. Luke is going to write up a block post on an alternative method that will allow you to extend this range for higher values. In the meantime, here’s what he’s outlined that seemed to work well.
1) Add a geogrpahic field, like State to get the generated lat/long. You can create one if needed.
3) Add this field for more layers. In fact, add a few extra because you won’t be able to add more later, and you can always remove them.
4) Remove the generated lat/long from Columns and Rows.
5) Duplicate each measure and set the geogrpahic property to latitude or longitude.
6) add them to the Columns and Rows (Latitude to Columns and Longitude to Rows so that you get a scatter plot).
7) Remove the geographic field from the Marks card.