Lecture 4: Visualisation Principles and Tableau II

STA1000 - Data Analytics and Metrics

Damien Dupré

Previously …

  • In Lecture 3 we matched each question to a family of figures (composition, distribution, comparison, relationship) and built each family in Tableau with insta-data.csv.

Today: make figures clear and honest, spot the misleading ones, then prepare the table behind them in Tableau: pivot it, add calculated fields, and merge it with a second file.

1. Customise your Figures

Customise Figures

  • Colours
  • Size of the dots
  • Shapes
  • Position (stack or dodge)
  • Orientation (vertical or horizontal)
  • Text content and font
  • Figure legend

Use Figures

Use figures only if they are self-explanatory, help to understand your message (a figure is not a message)

Don’t duplicate the information

  • Always give the key information in the text of your report, but
  • don’t show a table and a figure with the same information

Tell a story! Build a narrative around your figures

  • A set of observations, facts or events, presented in an order that creates a reaction in the audience
  • Even if you think data are boring, make them interesting for your audience

Fundamentals of Data Visualization

Fundamentals of Data Visualization by Claus O. Wilke

Free online at clauswilke.com/dataviz

It sets out the basic principles of data visualisation. The next slides take five of them, each shown as a bad figure next to a better one.

Principle 1: Axes

Don’t cheat with axes: include 0 when 0 is meaningful

A bar shows its value with its length, so it has to start at zero.

Principle 1: Axes

A shaded area shows its value too: it also starts at 0

To zoom in on small changes, drop the shading and keep the line.

Principle 1: Axes

Panels you compare must share the same axis

With its own axis in each panel, a 2-point change looks as big as a 10-point one.

Principle 2: Make It Easy

Make your figure easy to read and to understand

The same five numbers: sorted bars let you compare Kent and Washington at a glance.

Principle 3: Be Fancy

Use extra features when they help, such as transparency and jitter

When points pile up, even transparency is not enough: count them in small hexagons instead.

Principle 3: Be Fancy

Use extra features when they help, such as transparency and jitter

Transparency and a little jitter reveal the points hidden behind each other.

Principle 4: Colours

Use colours, but not too many

51 colours need a legend nobody can read: group them, and label the points that matter.

Principle 4: Colours

Keep the same colours for the same variables

And check the palette for colour-blind readers: about 1 man in 12 cannot tell red from green.

Principle 5: Details

Every detail counts, and lazy work is easily spotted

Label the lines directly: a legend sends the eye back and forth, here in a different order from the lines.

Principle 5: Details

Be precise, and use the title, axes and captions to do it:

  • a title and a subtitle that state the finding
  • axis titles with their units
  • labels on the few points worth naming
  • sensible precision: 10.4%, not 10.4056%
  • the data source

Your Turn!

What’s wrong here?

For each of the next charts find what is wrong

What’s Wrong Here?

Principle 1. The axis starts at 34%, so a rise from 35% to 39.6% is drawn as a bar more than five times taller.

Source: Misleading Graphs in Real Life

What’s Wrong Here?

Principle 1. The steps of the axis are uneven (30, 60, 90, 100, 130, 160, 190, 240, 250, 300, …): equal distances do not mean equal numbers, and the growth looks steadier than it was.

Source: The Worst Covid-19 Misleading Graphs

What’s Wrong Here?

Principle 1. Each panel has its own scale, so Gutiérrez’s 19% is drawn higher than Petro’s 37%. And December, January and March are equally spaced, although February is missing.

What’s Wrong Here?

Principles 2 and 3. A pie shows the parts of one whole, but these slices add up to 193%, and the 3D tilt distorts their sizes.

Source: The statisticians at Fox News use classic and novel graphical techniques to lead with data

What’s Wrong Here?

Principles 4 and 5. The rainbow colours mean nothing, and three bars are labelled 336 with three different heights: 2020 and 2021 are drawn taller than the 382 of 2016.

Source: KCRA graph on the 7pm news, r/Sacramento

What’s Wrong Here?

Principle 5. The last point, 8.6%, is drawn at the level of 9.0%. Placed correctly (right), November is the largest change of the year.

What’s Wrong Here?

Relationship is not causation. Two series that move together need not be linked: Nicolas Cage films do not drown people. The two y-axes (Principle 1) also let you make any two lines look alike.

Source: Nick Cage Movies Vs. Drownings, and More Strange (but Spurious) Correlations

Land Doesn’t Vote, People Do

A county map colours land. Turn each county into a circle sized by its number of voters and the same result tells a very different story.

Sources: Fact checking Trump’s ‘Impeach this’ map and Land Doesn’t Vote, People Do

A Must Read

“Mistakes, we’ve drawn a few”: The Economist learning from its own errors in data visualisation

The original breaks Jeremy Corbyn’s bar to fit it on the axis, so 5,210 likes is drawn only a little longer than the Labour Party’s bar (Principle 1). The better version gives every bar the same scale, in thousands.

Source: Mistakes, we’ve drawn a few, The Economist on Medium

Your Turn!

Ask an AI what’s wrong

  1. Copy this chart: ⊞ Win + ⇧ Shift + S or ⌘ Cmd + ⌃ Control + ⇧ Shift + 4
  2. Paste it into an AI assistant and ask: “What is wrong with this chart?”
  3. Compare with your own answer: what did it find that you missed? What did it miss? Did it invent a fault?

What’s Wrong Here?

  • The y-axis is inconsistent. It shows 0.5-point intervals from 6.0 to 5.0, then switches to 1-point intervals. This distorts the 2021 bar.
  • The chart only shows 2001 to 2021, so it cannot demonstrate the claim “since 1984”. The years 1984 to 2000 are missing.
  • The 5.7% figure is real GDP growth, not a general measure of economic performance. The BEA reports 5.7% growth in 2021 after GDP fell 3.4% in 2020, so the result partly reflects a post-pandemic rebound and a low comparison base.
  • 2021 is a calendar year, not entirely Biden’s first year in office, so attributing all of that growth to his presidency is misleading.
  • The oversized white bar and callout visually promote the desired conclusion rather than presenting the comparison neutrally.

Source: Official Twitter account of The White House

3. Transform your Data in Tableau

Data Transformation in Tableau

If your data is a stand-alone file, prefer transformations with Excel …

… except for pivoting your table (long versus wide) and joining two or more tables.

If your data updates itself (a cloud-hosted spreadsheet, a database, a connector), transforming in Tableau is required: the transformation runs again at every refresh.

Tableau has a dedicated tool for data transformation, Tableau Prep, but a lot can already be done in Tableau Public.

The Key Transformations in Tableau

Lecture 2 In Tableau
1. Extension: create a new column a calculated field
2. Reduction: keep only certain values a filter, on the Filters shelf or the Data Source page
3. Direction: arrange the row order a sort, from the toolbar or the axis
4. Aggregation: summarise a column the aggregation of a Measure: SUM, AVG, MEDIAN, …
5. Combination: join two tables a relationship or a join (section 4)


And long or wide? becomes a pivot, on the Data Source page.

Long or Wide?

Lecture 2: the same data can be wide, one column per quarter, or long, one row per region and quarter.

Tableau prefers long: one variable, one column, which you can put on Colour, Columns or Filters. In Excel this arrow takes a lot of copy and paste; in Tableau it takes one click: Pivot.

Pivot in Tableau

Wide: insta-data.csv as exported

post_id reach_followers reach_non_followers
LR0001 190 545
LR0003 7,737 2,656

Long: after the pivot

post_id audience audience_reach
LR0001 reach_followers 190
LR0001 reach_non_followers 545
LR0003 reach_followers 7,737
LR0003 reach_non_followers 2,656
  1. On the Data Source page, select both columns: Ctrl + click or ⌘ Cmd + click on their names
  2. Click the drop-down arrow of one of them → Pivot
  3. Rename Pivot Field Names as audience and Pivot Field Values as audience_reach


Each post now has two rows and every other value appears twice: count posts with CNTD (count distinct), and do not SUM the other columns.

Calculated Fields

A calculated field is Lecture 2’s Extension: a new column, computed from the others.

  • Analysis → Create Calculated Field, or the drop-down arrow at the top of the Data pane
  • Name it, type the formula, and wait for The calculation is valid
  • Fields go in square brackets, under the names Tableau shows (e.g., reach_followers appears as Reach Followers)

[Likes] + [Comments] + [Saves] + [Shares]

Named Engagements, it gives every post its number of interactions, and appears in the Data pane with an = before its icon.

Row Level versus Aggregate

Row-level calculation

Evaluated once per row, before aggregation.

[Engagements] / [Reach]

computed post by post, then averaged (AVG, not the default SUM), gives you the average of the rates: 6.4%.

Aggregate calculation

Evaluated after aggregation.

SUM([Engagements]) / SUM([Reach])

gives you the rate of the totals: 5.7%, which is almost always what you meant.

Your Turn!

Pivot and calculate

In a new workbook with insta-data.csv:

  1. Pivot reach_followers and reach_non_followers into audience and audience_reach
  2. Create Engagements ([Likes] + [Comments] + [Saves] + [Shares]), then the engagement rate by format

4. Merge your Data in Tableau

Connect to Multiple Data

It is possible to add more than one file but it can be done with or without joining the data.

  • If you join the two or more data source, you will be able to use variables from different files in the same visualisation. However, a relationship between the data has to be established.
  • If you don’t join the data, the variables from each data will have to be used separately in different visualisations.

Relationships versus Joins

The default method in Tableau is to use relationships: they preserve each table’s level of detail, and they are the recommended way to combine data in most cases. A join gives you more control, for instance to filter or to duplicate rows on purpose.

Relationship (the default)

  • Drag the second table next to the first: a line, the “noodle”, links them
  • Each table keeps its own rows
  • Tableau combines them for each view, only when the view needs both

Join

  • Double-click the first table, then drag the second one inside it
  • The two tables become a single table, before any view
  • You choose which rows survive: inner, left, right or full outer

Relating Multiple Data

  1. Connect your first data file/database. In the Data Source window, beside Connections, click on the plus sign to “Connect to Data”
  2. Drag and drop the second file next to the box representing the first file
  3. Set the variables that link the two files, here Employee Code in both: the line between the two boxes is the relationship

Inner and Left Joins

Inner: only the keys found in both tables, here 1 and 2

Left: every row of the left table; 3 has no match, so its y stays empty

x is the left table and y the right one. The animations write the joins in R; Tableau’s join dialog offers the same four. Source: Garrick Aden-Buie, tidyexplain

Right and Full Outer Joins

Right: every row of the right table; 4 has no match, so its x stays empty

Full outer: every row of both tables, empty wherever a key has no match

Source: Garrick Aden-Buie, tidyexplain

Which Rows Survive?

insta-posts.csv has one row per post (431), meta-ads.csv one row per boosted post (127). Combined on post_id:

Combination Keeps Rows
Inner join the posts found in both files 127
Left join every post, with an empty spend for the 304 organic ones 431
Right join every boosted post 127
Full outer join everything 431
Relationship both tables as they are, combined view by view 431 and 127

Check the number of rows after every join: it is shown above the data grid of the Data Source page. If it is not the number you expected, the join has changed your data.

When a Key Appears Twice

Key 2 appears twice in y, so the join copies x2: 4 rows instead of 3.

Every value of x2 now counts twice, and no error message warns you.

A relationship does not copy rows: one more reason it is Tableau’s default.

In Lecture 5, a join with Loomrow’s ads by country turns €24,345.78 of spend into €106,899.36.

Source: Garrick Aden-Buie, tidyexplain

Your Turn!

Merge Instagram and Meta Ads

From Loop, download insta-posts.csv and meta-ads.csv:

  1. Connect insta-posts.csv, then drag meta-ads.csv next to it: a relationship on post_id
  2. How many posts? How much was spent?
  3. Return on ad spend by format, as a rate of the totals
  4. Replace the relationship with a join: how many posts now? What can you no longer answer?

Thanks for your attention and don’t hesitate to ask if you have any questions!

@damien_dupre

@damien-dupre

https://damien-dupre.github.io

damien.dupre@dcu.ie