Skip to Content
R Graphics Cookbook, 2nd Edition
book

R Graphics Cookbook, 2nd Edition

by Winston Chang
November 2018
Beginner to intermediate content levelBeginner to intermediate
441 pages
9h 25m
English
O'Reilly Media, Inc.
Content preview from R Graphics Cookbook, 2nd Edition

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

Problem

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

Solution

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
library(dplyr)

# 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)) +
  geom_point()
Figure 5-1. A basic ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

R Graphics Cookbook

R Graphics Cookbook

Winston Chang
R Cookbook, 2nd Edition

R Cookbook, 2nd Edition

JD Long, Paul Teetor
R Statistics Cookbook

R Statistics Cookbook

Francisco Juretig

Publisher Resources

ISBN: 9781491978597Errata Page