6  Visualizing data

6.1 Why visualize?

In social sciences, we are often interested in exploring or testing a relation between two variables: Does poverty lead to lower health or life expectancy? Does social media use influence mental health? Do populist politicians use more polarizing speech? Very often, the focus for answering such questions is a statistical measure or test such as correlation, t-test, or regression. Such a test would tell us whether there is a (linear) relationship between the two variables.

In 1973, Francis Anscombe published a paper called Graphs in Statistical Analysis in which he famously shows why very often this statistical test doesn’t show the whole story.

Suppose you have two variables (e.g. poverty and life expectancy), and you find a very strong and highly significant correlation (\(r=0.82, p<.01\)). What would you conclude about the relationship between those variables?

Now, run the code below and think about your conclusion. How do graphs and correlation contribute to the understanding of the relation?

(Don’t worry about the specific code in the example - the ggplot code will be explained in detail in this chapter. And if you’re brave, feel free to peek into the data preparation box!)

Don’t worry about the data preparation below – but if you’re curious, the recode_values was explained in Section 5.2, group_by |> summarize was explained in Section 5.3, and the pivot_longer code will be explained in Section 7.1)

These graphs raise a number of intriguing questions. How do you interpret the relation between the variables as expressed in the graphs? All datasets have the same correlation strength and significance, why do they have such different shapes? What does that say about the difference between graphs and statistical tests of relationships?

This example powerfully showcases the need to explore data as well as computing statistical values, and this is in fact one of the two big use cases of data visualization (the other being telling the story of your analysis to readers).

In this case, (a) is probably what you expected the data to look like: a cloud of points dispersed randomly around the blue trendline; (b) shows a clearly non-linear relationship: as x increases, y first goes up, but then goes down towards the end; (c) shows how a single outlier can affect an otherwise very regular linear pattern; and (d) shows how a single outlier can create a (spurious) relationship that is not present in the other points at all. Crucially, the data behind all four plots showed the same underlying linear relationship, but the plots clearly show a completely different story in all four cases, and point at a modelling issue in all cases except the first.

Now that you are convinced of the need to visualization to explore your data (and tell your story to a reader), let’s explore how you can do this using ggplot!

6.2 Your first ggplot

6.3 Mapping more variables

6.4 How do they compare? Bar charts

6.5 Recreating a published figure

6.6 How is it distributed?

6.7 How does it change over time?

6.8 Comparing groups side by side: facets

6.9 Saving your plot

6.10 Learn more