Data + Science
Z-Order in Tableau
The z-order is the order of overlapping two-dimensional objects, for example, circles plotted on top of each other in a scatter plot. In CSS, there is a z-index setting that is used to bring an object forward or back within a view, controlling which object is plotted on top. In Tableau, we don’t explicitly define a z-order in that way. Instead, the view is defined by the marks that are being used. This can be confusing, and some times frustrating, when plotting marks on top of each. If a larger mark is plotted on top of a smaller mark, then it becomes impossible to hover or select the smaller mark. This affects tooltips, hover actions, or selecting a mark to highlight or filter. In this post I will outline how to control the z-order. Spoiler – its the pill order on the Marks Card that controls the z-order. Here are three examples.
Marks Ordered by the Color Legend
Below is a scatter plot of Life Expectancy of a Female and Infant Mortality using the World Indicators data in Tableau. I have plotted this scatter plot using Region (which is the continent) on color. I set the opacity on the Color to 55% so that bubbles on top of each other can be seen through some transparency of the color. Notice that Asia is the second color in the order of the legend. Notice that Region is the first pill in the Marks Card and set to Color, the order of the color legend controls the z-order.
This means that circles representing Africa are on top of the circles representing Asia and so on. In the image below you will see the large circle representing China is on top of all of the other Regions, except the small blue dots for the African countries.
You can control the order of the legend simply by dragging a Region higher or lower in the color Legend (or manually sorting the Region on the Marks Card).
You might be wondering about the other countries in Asia, which are at the same level in the color legend. To answer this, we can examine Thailand (in Asia) which is plotted underneath China. This is because the next z-order after Region is set by the next pill in the view, which is Country, and based on that order Thailand is below China in the list. Therefore China will plot on top of Thailand.
Marks Ordered by Country Alphabetically
In this next example, I simply moved Country from the bottom of the Marks Card to the top. Since Countries are ordered alphabetically and Country is at the top of the Marks Card, the z-order is now set to Countries in alphabetical order. In other words, Afghanistan will be the mark plotted on top and Zimbabwe will be plotted on the bottom.
Notice that Bulgaria (in Europe) and Brazil (in The Americas) are plotted on top of China, even though Asia is above them in the color legend. This is because Tableau is using the order of Country names, the top pill on the Marks Card, and not the color legend. Since the Russian Federation (in Europe) is below China in the list of countries, it’s plotted behind China in the z-order.
Marks by Country and Sorted by Population
We can control the sorting of the marks by sorting the pill at the top of the Mark’s Card. For example, if we want all of the smaller dots to be on top of the larger dots in every case, then we can sort the countries by population. If we sort by Population Total ascending, then the smallest countries by population will be at the top of the list and the largest countries will be at the bottom of the list, and the z-order will plot the smallest dots on top of the larger ones.
Now we can hover over every dot in the view, because the smaller dots are plotted on top of the larger dots, regardless of the country or region.
Below is a Tableau Visualization showing the three examples side-by-side. Hover over the dots in the three examples to see the difference based on the pill order and sorting on the Marks Card.
Read More …