Scatterplot

Before you start: vectors and data frames; row order.
Setup: Use built-in mtcars. Make the points plot before optional fitted/smoothed overlays.
Your check: Plot paired measurements and distinguish an observed association from causation. Practise in the unit summary.

What is a Scatterplot?

Comic scatterplot positioning foods by taste and ease of eating, rather than measured physical quantities.

A scatter plot is a graphical representation of observed pairs of values of two statistical characteristics. These pairs of values are plotted in a Cartesian coordinate system, resulting in a scatter plot. The points can be represented by different symbols. It is hoped that the pattern of points in the scatter plot will provide information about the dependency structure of the two features represented by the coordinates.

There are many ways to create a scatterplot in R. The basic function is plot(x, y), where x and y are numeric vectors denoting the (x,y) points to plot.

Example with mtcars:

plot(mtcars$wt, mtcars$mpg, main="Scatterplot Example",
     xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)

Optional preview: fitted and smoothed lines

The scatterplot above is sufficient for this unit’s core task. Continue here if you want to preview tools used in later analysis courses. First run that plot: abline(), lines() and legend() add to an existing graph.

lm(y ~ x) fits a straight-line model; the formula puts the response on the left and predictor on the right. The line below is a descriptive overlay, not evidence of causation. Drawing it does not check the model’s assumptions. Formal inference requires checking the model and its residuals; see R’s linear-model documentation.

# adding Regression lines and a legend
abline(lm(mtcars$mpg~mtcars$wt), col="steelblue") # regression line (y~x)
lines(lowess(mtcars$wt,mtcars$mpg), lty=2, col="darkgreen") # LOWESS/LOESS line (x,y)
legend("topright", legend=c("Regression", "LOWESS"),
       col=c("steelblue", "darkgreen"), lty=1:2, cex=0.8)
Scatterplot of mtcars weight against miles per gallon: heavier cars generally have lower mpg; linear regression and LOWESS curves are overlaid.
Simple scatterplot.

Info
LOWESS (Locally Weighted Scatterplot Smoothing) draws a descriptive smooth through the observed relationship. It is related to LOESS, but lowess() and loess() are different R functions. The smooth depends on its settings and does not establish causation or justify forecasting beyond the observed data. See the R documentation for lowess().

Test your knowledge!

Updated: