Recently I participated in the last Workout Wednesday of the year 2020 | Week 51 (Learn Why Workout Wednesday is important). The requirement was to create a Divergent histogram of Male & Female life expectancy by country. In the first look at data, it seemed easy and I started creating it but after I was finished with 95% of the chart, struggled to create the label on the chart, which was one of the requirements of this exercise. I further dig down into different ways of creating this chart and ended up with 2 approaches for which I discussed the steps below.
For both of the approaches, you need to pivot the data (which was allowed). I pivoted the "Life Expectancy Female" and "Life Expectancy Male" fields by using the inbuilt functionality of tableau.
The result is, we get 2 fields "Pivoted Field Names" and "Pivoted Field Values" which I renamed to "Sex" and "Life Expectancy" respectively.
Approach 1: Count of Countries.
1. Create a "Life Expectancy (Bin)" with a bin size of 1.
2. Create a "Male Female Count" calculation.
3. Drag "Life Expectancy (Bin)" to columns and "Male Female Count" to rows (both continuous).
4. Drag "Sex" to the color marks card.
5. Drag Year field to filter pan.
Male Female Count:
IF CONTAINS(ATTR([Sex]),"Male") THEN
-COUNT([Country/Region])
ELSE
COUNT([Country/Region]) END
For label of max of Males and Females.
1. Add another duplicate axis of "Male Female Count" on rows
2. Convert the 2nd Measure to a Gantt chart to show the labels
3. Change color opacity to 0.
4. Make both dual-axis and Synchronise axis.
The view is almost similar but the problem is your labels are not in place, we need them on the top of the bars.
For this purpose, I built the same graph with another approach.
Approach 2:
1. Create a "Life Expectancy (Bin)" with a bin size of 1.
2. Create 2 calculations for [Male] and [Female] numbers and drag them to Rows.
3. Drag "Life Expectancy (Bin)" to columns
4. Drag Measure Names to Color Marks Card.
5. Drag Year field to filter pan.
6. Show labels: Marks to Label - Min/max; Pane; Automatic Field; label Max Value.
[Male]:
IF CONTAINS([Sex],"Male") THEN 1 END
[Female]:
IF CONTAINS([Sex],"Female") THEN 1 END
Do some required formatting, adjust axis & tooltips.
Final View:
Check out both the views here on Tableau Public.
Post a Comment