Engineering

Scatter plot with trend line and r: car stopping distances

Scatter plot with a least-squares trend line, 95% confidence band and Pearson r for 50 cars: stopping distance against speed, numbers as in R lm and cor.test.

Open this example in the editorStart from scratch

YLine: least-squares fit; shaded: 95% confidence band (r = 0.807, n = 50)0501001500510152025Speed (mph)Stopping distance (ft)
Stopping distance against speed for 50 cars with a least-squares line, its 95% confidence band and r = 0.807.

About this example

R’s datasets::cars records the speed and stopping distance of 50 cars measured in the 1920s (Ezekiel 1930). The points rise steadily with speed, and the spread widens at higher speeds, which is visible because every car is drawn.

The line is the ordinary least-squares fit: distance = −17.58 + 3.93 × speed, so each extra mph adds about 3.9 ft (95% CI 3.10 to 4.77). Pearson’s r is 0.807 (95% CI 0.682 to 0.886, p < 0.001). These match R’s lm, confint and cor.test, and the downloadable R script reproduces them.

The shaded band is the 95% confidence band for the mean distance at each speed, not the range where individual cars fall; it is narrowest near the mean speed. A negative intercept shows that a straight line is only an approximation near zero speed, so the line is not extended beyond the observed 4–25 mph.

Sources: R datasets::cars — Ezekiel (1930)

The data

XY
42
410
74
722
816
910
1018
1026
1034
1117
1128
1214
1220
1224
1228

…and 35 more rows. Open the example to see all of them.

Source: R datasets::cars (Ezekiel 1930). The line is an ordinary least-squares fit with its 95% confidence band; r and its CI match R cor.test. Open the example, replace the data with your own, and export the figure as SVG, PDF, PNG or TIFF.

More examples