Learn R Programming  /  Base Graphics  /  Scatter Plot

Scatter Plot

A scatter plot shows the relationship between two numeric variables by plotting one point per observation — the go-to chart for spotting correlation or clusters.

A Basic Scatter Plot

R Code
height <- c(150,160,165,170,180)
weight <- c(50,55,60,65,75)

plot(height, weight,
     main = "Height vs Weight",
     xlab = "Height (cm)", ylab = "Weight (kg)",
     pch = 19, col = "steelblue")

Colouring Points by Group

R Code
gender <- c("M","F","M","F","M")
colours <- ifelse(gender == "M", "blue", "deeppink")

plot(height, weight, col = colours, pch = 19)
legend("topleft", legend = c("Male","Female"),
       col = c("blue","deeppink"), pch = 19)

Adding a Trend Line

R Code
plot(height, weight, pch = 19)
model <- lm(weight ~ height)   # fit a simple linear regression
abline(model, col = "red", lwd = 2)  # draw the fitted line

Advanced: Scatter Plot Matrix for Many Variables

🚀 Advanced Example
pairs() draws every possible pairwise scatter plot for a data frame at once - a fast way to scan for relationships across several numeric variables.
R Code
df <- data.frame(height, weight, age = c(20,25,30,35,40))
pairs(df, pch = 19, col = "steelblue")

Key Points

  • plot(x, y) with two numeric vectors produces a scatter plot automatically.
  • Colour points by a grouping variable using ifelse() or by mapping a factor to a colour vector.
  • abline(lm(y ~ x)) is the quickest way to overlay a fitted trend line.
  • pairs(df) draws a full grid of scatter plots for every pair of numeric columns.