2.1 Introduction

Above all else show the data.

Edward R. Tufte (2001)

Why should we visualize data? Most people intuitively assume that visualizations help our understanding of data by illustrating or emphasizing certain aspects, facilitating comparisons, or clarifying patterns that would otherwise remain hidden. Precisely justifying why visualizations may have these benefits is much harder (see Streeb, El-Assady, Keim, & Chen, 2019, for a comprehensive analysis). And it would be naive to assume that visualizations are always helpful or appropriate. Instead, we can easily think of potential problems caused by visualizations and claim that they frequently distract from important aspects, facilitate misleading comparisons, or obscure and hide patterns in data. Thus, visualizations are representations that can be good or bad on many levels. Creating effective visualizations requires a mix of knowledge and skills that include aspects of human perception, psychology, design, and technology. Being an artificial object that is designed to serve particular purposes, good questions for evaluating a given visualization include:

  • What is the message and the audience for which this visualization was designed?
  • How does it convey its message? Which aesthetic features does it use? Which perceptual or cognitive operations does it require or enable?
  • How successful is it? Are there ways in which its functionality could be improved?

This chapter is mostly concerned with particular tools and technologies for creating visualizations. And rather than evaluating visualizations, we focus on creating some relatively simple types of graphs. But the insight that any representation can be good or bad at serving particular purposes is an important point to keep in mind throughout this chapter and book. To still provide a tentative answer to our initial question (“Why visualize data?”), the following sections will motivate our efforts with a notorious example.

2.1.1 Motivation

Consider the following example: You have some dataset that is split into four subsets of 11 points (i.e., pair of x- and y-coordinates). To get the data, simply evaluate the followning code (which you do not need to understand at this point — just copy and run it to follow along):

This code creates a data table ans (as a “tibble”) and splits it into four separate subsets (a_1 to a_4). We can use our basic knowledge of R to examine each subset, starting with a_1:

Checking a_2, a_3 and a_4 in the same way reveals that all four subsets have the same variable names (x and y) and the same shape (i.e., each object is a tibble with 11 rows and 2 columns).

Basic statistics

What do psychologists normally do to examine data? One possible answer is: They typically use statistics to summarize and understand data. Consequently, we could compute some basic statistical properties for each set:

  • the averages of both variables (e.g., means of \(x\) and of \(y\));
  • their standard deviations (\(SD\) of \(x\) and of \(y\));
  • the correlations between \(x\) and \(y\);
  • a linear model (predicting \(y\) by \(x\)).

For the first subset a_1, the corresponding values are as follows:

Practice

Compute the same measures for the other three subsets (a_2 to a_4). Do they seem similar or different?

Same stats, but…

The following Table 2.1 shows that — except for some minor discrepancies (in the 3rd decimals) — all four subsets have identical statistical properties:

Table 2.1: Statistics of the 4 subsets (rounded to 2 decimals).
nr n mn_x mn_y sd_x sd_y r_xy intercept slope
1 11 9 7.5 3.32 2.03 0.82 3 0.5
2 11 9 7.5 3.32 2.03 0.82 3 0.5
3 11 9 7.5 3.32 2.03 0.82 3 0.5
4 11 9 7.5 3.32 2.03 0.82 3 0.5

Does this imply that the four subsets (a_1 to a_4) are identical? Although our evidence so far may suggest so, this impression is deceiving. In fact, each of the four subsets contains different values. This could be discovered by examining the actual values of all four subsets (e.g., by printing and comparing the four tables).

By contrast, visually inspecting the data makes it far easier to see what is really going on. Figure 2.2 shows four scatterplots that shows the x-y coordinates of each subset:

Scatterplots of the four subsets in the anscombe dataset.

Figure 2.2: Scatterplots of the four subsets in the anscombe dataset.

This example illustrates that highly similar statistics can stem from very different datasets. Consequently, we should never rely exclusively on a statistical analysis. Statistics is a useful tool for testing scientific hypotheses, but to fully understand data, we should always strive to visualize it in meaningful ways.

2.1.2 Objectives

After working through this chapter, you should be able to:

  1. explain why we should always aim for a transparent visualization of data;
  2. know the basic structure of a ggplot command;
  3. distinguish between various types of plots (e.g., scatterplots, histograms, bar plots, line graphs);
  4. use different geoms to create these plots;
  5. adjust the aesthetic properties (e.g., colors, shapes) of plots;
  6. adjust the axes, legends, and titles of plots.

2.1.3 Getting ready

This chapter formerly assumed that you have read and worked through Chapter 3: Data visualization of the r4ds textbook (Wickham & Grolemund, 2017). It now can be read by itself, but reading Chapter 3 of r4ds is still recommended.

Based on this background, we examine some essential commands of the ggplot2 package in the context of examples. Please do the following to get started:

  • Create an R script (.R) or an R Markdown (.Rmd) document (see Appendix F and the templates linked in Section F.2) and load the R packages of the tidyverse and the ds4psy package.

  • Structure your document by inserting headings, meaningful comments, and empty spaces between different parts. Here’s an example that shows how your initial file could look:

  • Save your file (e.g., as 02_visualize.R or 02_visualize.Rmd in the R folder of your current project) and remember saving it regularly as you keep adding content to it.

To learn to use the ggplot2 package, we first need to understand the structure of ggplot() calls.

References

Streeb, D., El-Assady, M., Keim, D., & Chen, M. (2019). Why visualize? Untangling a large network of arguments. IEEE Transactions on Visualization and Computer Graphics. https://doi.org/10.1109/TVCG.2019.2940026

Tufte, E. R. (2001). The visual display of quantitative information (2nd ed.). Cheshire, CT: Graphics Press.

Wickham, H., & Grolemund, G. (2017). R for data science: Import, tidy, transform, visualize, and model data. Retrieved from http://r4ds.had.co.nz