R Graphics Ā· Scatterplot

Investigating Relationships with Scatterplots in R

Explore associations, fitted lines, groups, size encodings, nonlinear smoothers, and marginal information.

6 examplesReproducible R code
Before you begin

Choose the variation that serves the analysis

Scatterplots show paired numerical observations. They reveal direction, strength, form, clusters, and unusual points, making them an essential first step before modeling a relationship.

Examples
06
Levels
Beginner + Intermediate
01
Example 01Base RBeginner

Create a Basic Scatterplot

A scatterplot places one quantitative variable on each axis.

Create a Basic Scatterplot example generated in R
R-generated example output.

Choose variables deliberately and label both axes with their units so the relationship is interpretable.

RRun this code
plot(mtcars$wt, mtcars$mpg, pch = 19, col = "#2563eb",
     xlab = "Weight (1000 lb)", ylab = "Miles per gallon")
02
Example 02Base RBeginner

Add a Linear Regression Line

A fitted line summarizes the average linear relationship between two variables.

Add a Linear Regression Line example generated in R
R-generated example output.

abline() draws the fitted model, but the points remain essential for identifying nonlinearity and influential observations.

RRun this code
plot(mtcars$wt, mtcars$mpg, pch = 19, col = "#2563eb")
model <- lm(mpg ~ wt, data = mtcars)
abline(model, col = "#dc2626", lwd = 3)
03
Example 03Base RBeginner

Color Points by Group

Color can expose subgroup clusters and group-specific relationships.

Color Points by Group example generated in R
R-generated example output.

Map factor levels to a small palette and include a legend. Do not rely on color alone when accessibility is critical.

RRun this code
cols <- c("#2563eb", "#0891b2", "#f59e0b")[iris$Species]
plot(iris$Sepal.Length, iris$Petal.Length, pch = 19, col = cols)
legend("topleft", levels(iris$Species), col = c("#2563eb", "#0891b2", "#f59e0b"), pch = 19, bty = "n")
04
Example 04Base RIntermediate

Encode a Third Variable with Size

A bubble plot varies point size to display an additional quantitative variable.

Encode a Third Variable with Size example generated in R
R-generated example output.

Scale areas—not radii—proportionally and keep the size range moderate so small values remain visible.

RRun this code
sizes <- sqrt(mtcars$hp / pi) / 3
plot(mtcars$wt, mtcars$mpg, pch = 21, bg = adjustcolor("#2563eb",.55),
     cex = sizes, xlab = "Weight", ylab = "MPG")
05
Example 05Base RIntermediate

Reveal a Nonlinear Pattern

A smooth curve summarizes relationships that are not well represented by a straight line.

Reveal a Nonlinear Pattern example generated in R
R-generated example output.

lowess() is exploratory; its flexibility should be chosen with care and reported when used analytically.

RRun this code
x <- cars$speed; y <- cars$dist
plot(x, y, pch = 19, col = adjustcolor("#2563eb",.5))
lines(lowess(x, y), col = "#dc2626", lwd = 3)
06
Example 06ggplot2Intermediate

Create a Scatterplot with ggplot2

ggplot2 combines visual mappings, points, fitted models, labels, and themes in layers.

Create a Scatterplot with ggplot2 example generated in R
R-generated example output.

geom_smooth(method = "lm") adds a fitted line and confidence band while keeping the mapping reproducible.

RRun this code
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
  geom_point(color = "#2563eb", size = 2.5) +
  geom_smooth(method = "lm", color = "#dc2626") +
  labs(x = "Weight", y = "Miles per gallon") + theme_minimal()