Chapter 5. Scatter Plots

Scatter plots are used to display the relationship between two continuous variables. In a scatter plot, each observation in a data set is represented by a point. Often, a scatter plot will also have a line showing the predicted values based on some statistical model. Adding this line is easy to do with R and the ggplot2 package, and can help to make sense of data when the trends aren’t immediately obvious just by looking at the points.

With large data sets, plotting every single observation in the data set can result in overplotting, when points overlap and obscure one another. To deal with the problem of overplotting, you’ll probably want to summarize the data before displaying it. We’ll also see how to do that in this chapter.

5.1 Making a Basic Scatter Plot


You want to make a scatter plot using two continuous variables.


Use geom_point(), and map one variable to x and one variable to y.

We will use the heightweight data set. There are a number of columns in this data set, but we’ll only use two in this example (Figure 5-1):

library(gcookbook) # Load gcookbook for the heightweight data set

# Show the head of the two columns we'll use in the plot
heightweight %>%
  select(ageYear, heightIn)
#>     ageYear heightIn
#> 1     11.92     56.3
#> 2     12.92     62.3
#> 3     12.75     63.3
#>  ...<230 more rows>...
#> 235   13.67     61.5
#> 236   13.92     62.0
#> 237   12.58     59.3

ggplot(heightweight, aes(x = ageYear, y = heightIn)) +
Figure 5-1. A basic ...

Get R Graphics Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.