9/29/2016
How To Recreate a Pew Research Center Chart in Tableau

Last week, one of my students asked if a chart done by the Pew Research Center could be recreated in Tableau, here is a link to the chart. Below is my recreation of this chart in Tableau and a quick description of how this was done.

Note: I'm not advocating that this is a good way to show the data. See commentary at the bottom of this post.

If you would like to follow along, download the Excel file here for the data and my finished TWBX workbook here.

### Step 1 - Putting the Data Together

There are two chart components. The dot plot and diverging bar chart on the left side and the bar chart on the right side. The data for the left side is pretty straight forward. I created columns for the data, one category each for tilting Republican, tilting Democrat and the Total.

The data looks like this:

The data for the right side is a bit trickier. The bars are offset by +/- 9%. The Republican chart starts at +9% as the baseline and the Democrat chart starts at -9%. This is accomplished by using a stacked bar. A white bar for the spacer on the Republican chart and -9% spacer in blue for the Democrat chart. Notice the data is 9% less than the label, so I created a field specifically for the label, which is the data +9%.

Note: the Spacer for the offset and the D-Label could also be done in Tableau using calculated fields or a parameter.

### Step 2 - Build the First Chart (left side)

Before building the diverging bar chart and dot plot, we need to create a new calculated field. The calculation will simply check the affiliation and if it's Democrat then it will apply a negative value. This will make the bar chart go negative for Democrat and positive for Republican.

Create calculated field [Affiliated Plot]:
if [Party]='Democrat/Lean Democrat' then -[Affiliation]
else [Affiliation]
end

Create calculated field [Label]:
[Affiliation]*100

Place the new field Affiliation Plot on Columns and Group on Rows.
Place Party on Color. Set the colors for Democrate (blue) and Republican (red).
Place Tilt on Filters and filter for Republican and Spacer.
Duplicate Affiliation Plot on Columns.
Create a Dual Axis and Right-Click the top axis and select Synchronize Axis.
Set the first marks card to Bars and on Color, set transparency to 30%.
Add Label to Label on the second marks card which should be set on Circle.
Hide X-Axis.

### Step 3 - Building the Second Chart (right side)

Set the new marks card to Bars.
Place Tilt on Colors for the new marks.
Add Spacer to the Filters to create a stacked bar chart.
Change the color of the Spacer to white and Republican to Red.
Set the X-axis range to be fixed from -.1 start to 1.2 end.

Duplicate the entire sheet again and change the Filters to filter for Total only.
Duplicate the entire sheet and change the Filters to filter for Democrat only.
Replace Label with D-Label for the label on Democrat.

You now have the 3 charts for Total, Republican and Democrat.

Use Reference Lines on the right side bar charts to show the offset bar chart lines.
On the Republican Chart, remove all lines and add a Reference Line at 0% and 9%.
On the Democrat chart, remove all lines and add a Reference Line at 0% and -9%.
On the Total chart, remove all lines and add a Reference Line at 0%

All that's left is to add some formatting. Then place it all together on a Dashboard with some text boxes and it's done.

I find the +/- 9% very confusing. It's hard to get a sense of the shift across parties in this view. Comparing things on a common baseline is something that humans do very well. It's easy, perceptually, to make very accurate comparisons. In this case, we have bars moving in the same direction, from left to right, but they are offset, one by -9% and one by +9%, make the gap between them a total of 18%. If the point of the chart is to show how each group is tilting politically, then I think there are better ways to show this.

One option would be a stacked bar crossing the middle. Below is a similar chart by FiveThirtyEight showing how states are leaning for the election. They show a very similar chart on the left hand side and then simply list the advantage as a number on the right hand side, colored to show the party tilt.

A bar chart is certainly helpful to see the data, but if using them then having them on a common baseline would be better. There is no need to visualize this data with an offset.

Here are two alternatives to show the tilt using bar charts that are not offset.

This one using a diverging bar in the same direction as the other chart.

I added this commentary because I wanted to point out that recreating this chart in Tableau does not mean I am advocating showing data in this manner.

I hope you find this information helpful. If you have any questions feel free to email me at Jeff@DataPlusScience.com

Jeffrey A. Shaffer