R Scatterplot
A scatter plot shows the relationship between two numeric variables: each observation becomes one point. It is the first thing to draw when you suspect that one variable depends on another.
plot(mtcars$wt, mtcars$mpg,
main = "Heavier cars use more fuel",
xlab = "Weight (1000 lbs)", ylab = "Miles per gallon",
pch = 19, col = "steelblue")
cor(mtcars$wt, mtcars$mpg)[1] -0.8676594
The points fall from left to right: heavier cars drive fewer miles per gallon. cor() measures the strength of that linear relationship from -1 (perfect negative) to +1 (perfect positive).
The formula interface
Many R functions accept a formula y ~ x ("y explained by x") together with a data argument. It reads naturally and saves typing mtcars$ every time:
plot(mpg ~ hp, data = mtcars, pch = 17, col = "darkred",
main = "Horsepower vs. fuel economy")
range(mtcars$hp)[1] 52 335
Colouring points by group
Pass a vector of colours, one per point. Indexing a colour palette by a factor's codes is a neat trick:
cols <- c("tomato", "seagreen", "royalblue")
plot(Petal.Width ~ Petal.Length, data = iris,
col = cols[iris$Species], pch = 19,
main = "Iris petals by species")
legend("topleft", legend = levels(iris$Species), col = cols, pch = 19)
table(iris$Species)
setosa versicolor virginica
50 50 50
Because iris$Species is a factor with levels setosa, versicolor and virginica, cols[iris$Species] gives every setosa flower the first colour, and so on.
Adding a trend line
lm() fits a straight line through the points (see the Linear Regression lesson), and abline() can draw it directly:
fit <- lm(dist ~ speed, data = cars)
plot(dist ~ speed, data = cars, pch = 1,
main = "Stopping distance of cars (1920s)")
abline(fit, col = "red", lwd = 2)
round(coef(fit), 3)(Intercept) speed
-17.579 3.932
Comparing two sets of points
Use points() to add a second group to an existing scatter plot:
set.seed(7)
x1 <- rnorm(20, mean = 5); y1 <- x1 + rnorm(20, sd = 0.5)
x2 <- rnorm(20, mean = 7); y2 <- 12 - x2 + rnorm(20, sd = 0.5)
plot(x1, y1, xlim = range(x1, x2), ylim = range(y1, y2),
pch = 19, col = "orange", xlab = "x", ylab = "y", main = "Two groups")
points(x2, y2, pch = 15, col = "purple")
round(c(group1 = cor(x1, y1), group2 = cor(x2, y2)), 2)group1 group2
0.95 -0.90
A scatter plot matrix
pairs() draws a scatter plot for every pair of columns, which is a fast way to explore a whole dataset:
pairs(mtcars[, c("mpg", "wt", "hp", "qsec")], pch = 20, col = "grey30")
round(cor(mtcars[, c("mpg", "wt", "hp", "qsec")]), 2) mpg wt hp qsec
mpg 1.00 -0.87 -0.78 0.42
wt -0.87 1.00 0.66 -0.17
hp -0.78 0.66 1.00 -0.71
qsec 0.42 -0.17 -0.71 1.00
Test yourself
What does plot(y ~ x, data = df) do?
- Plots x against y with y on the x-axis
- Draws a scatter plot with x horizontal and y vertical
- Fits a regression
- Draws a histogram of y
A correlation of -0.87 means…
- No relationship
- A strong negative linear relationship
- A weak positive relationship
- An error in the data
Which function draws scatter plots for every pair of columns?
- plot.all()
- pairs()
- matrix()
- scatter()