How to Create a Diverging Stacked Bar Chart

Do you have survey data? Do you want an interesting way to show the negative versus positive responses? Use a Diverging Stacked Bar Chart!


Here is an example:



This Kaggle link contains data from UK student survey in 2013. I will just focus on the music data for this Tableau chart.


The criteria for the music portion are the following;

  1. I enjoy listening to music.: Strongly disagree 1-2-3-4-5 Strongly agree (integer)

  2. I prefer.: Slow paced music 1-2-3-4-5 Fast paced music (integer)

  3. Dance, Disco, Funk: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  4. Folk music: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  5. Country: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  6. Classical: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  7. Musicals: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  8. Pop: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  9. Rock: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  10. Metal, Hard rock: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  11. Punk: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  12. Hip hop, Rap: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  13. Reggae, Ska: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  14. Swing, Jazz: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  15. Rock n Roll: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  16. Alternative music: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  17. Latin: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  18. Techno, Trance: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

  19. Opera: Don't enjoy at all 1-2-3-4-5 Enjoy very much (integer)

I removed the first and second options because I didn't think they were pertinent to the visualization.


I took this dataset through Alteryx removing all of the other columns that didn't pertain to music. I assigned a Record ID field to each row and then transposed the data to a tall format. Tableau works faster with tall data (meaning many rows) versus wide data (many columns).


You can see the finalized dataset in the image below;


I connected to this dataset in Tableau Public and created the following calculated fields;


Strongly Agree

SUM(IF [Value]=5 then 1 Else 0 END)/(CountD([RecordID]))


Agree

SUM(IF [Value]=4 then 1 Else 0 END)/CountD([RecordID])


Disagree

SUM(IF [Value]=2 then -1 Else 0 END)/CountD([RecordID])


Strongly Disagree

SUM(IF [Value]=1 then -1 Else 0 END)/CountD([RecordID])


Then I had to cover the 3 scores which were neutral. These could be neither good, or bad so I had to create two calculations for these;


NeutralNegative

SUM(IF [Value]=3 then -1 Else 0 END)/(2*CountD([RecordID]))


NeutralPositive

SUM(IF [Value]=3 then 1 Else 0 END)/(2*CountD([RecordID]))


In order to create the stacked bar chart, I needed to move Measure Values to the Columns shelf and then move the Name pill to the rows shelf. The Name field represents all of the music genres.


From the Measure Values marks card, I put only the calculated fields that I created. I removed all other measures.



Next I needed to add Measure Names to the Color marks card. I also moved around the pills on the Measure Values card so the values would appear in the below order across the chart;


Strongly Disagree, Disagree, NeutralNeg, NeutralPos, Agree, Strongly Agree


Then I needed to format the axis as a percentage. I right-clicked on the axis to do so.


I then made the changes to the Numbers group for both the Axis and Pane tabs in the Format Measure Values menu on the left hand side.



Next I changed the colors to the Color Blind palette using the most pale colors as my NeutralPositive and NeutralNegative pills.



I changed the View from Standard to Entire View.

I hid the field labels for the rows.

I deleted the Title on the Axis.

I changed the Font for the left Axis to be Bold and Font Size of 10.

I added the Value pill to the Columns shelf and changed the calculation to Average.

I selected Dual Axis by left-clicking to select this option on this pill.

I had to change the Measure Values marks card back to Bar.

I right-clicked on the top axis and selected Edit Axis.

I changed the Range to Fixed and put 0 for the starting point.

I entered 4 for the end.

I deleted the Title.

*I did not synchronize the axes!


On the AVG(Value) Marks card I performed the following steps;

  • Increased the size of the circle

  • Changed its color to gray by removing the Measure Names pill

  • Selected a black border for the circle, by selecting this option in the Color marks card drop-down

  • Dragged the Avg (Value) measure to the Label marks card

  • Changed the alignment of the Avg (Value) to be center aligned

  • Changed the format of the Avg(Value) text to be 1 decimal place

I then hid the top axis by right-clicking on it and de-selecting Show Header.

I removed all of the row dividers and column dividers. I also removed the pane and header lines.

I then sorted this chart by the Avg(Value) on the Columns shelf to display the most favored music genre to the least favored.


For the last steps, I cleaned up the tooltips to display as below;


For the Circles

<Name> Music Genre

<AVG(Value)> Avg Score


For the Bars

<Name> Music Genre

<Measure Names>: <Measure Values>


This chart can be found in my Tableau Public dashboard here under the Deviation section.

126 views0 comments