How to create a scatter plot with ggplot2 in R

· 2 min read · Updated March 15, 2026 · beginner
r ggplot2 visualization scatter-plot graphics

Scatter plots display the relationship between two continuous variables. In ggplot2, you create them with geom_point().

Basic scatter plot

Load ggplot2 and create a simple scatter plot:

library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point()

This displays car weight (wt) versus miles per gallon (mpg).

Adding color by group

Color points by a categorical variable:

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point() +
  labs(color = "Cylinders")

The factor(cyl) converts the cylinder count to a categorical variable.

Changing point size and shape

Customize point appearance with size and shape:

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(size = 3, shape = 17)  # triangle point

Common shapes: 16 (circle), 17 (triangle), 18 (diamond).

Mapping aesthetics to variables

Set aesthetics inside aes() to map them to data:

ggplot(mtcars, aes(x = wt, y = mpg, size = hp, color = factor(cyl))) +
  geom_point(alpha = 0.7)
  • size = hp scales point size by horsepower
  • alpha = 0.7 makes points semi-transparent

Adding trend lines

Overlay a smoothing curve to show trends:

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE)  # linear trend line

Use method = "loess" for local polynomial smoothing.

Labels and titles

Add proper labeling with labs():

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point() +
  labs(
    title = "Car Weight vs Fuel Efficiency",
    subtitle = "By number of cylinders",
    x = "Weight (1000 lbs)",
    y = "Miles per Gallon",
    color = "Cylinders"
  )

Faceting for multiple plots

Create side-by-side plots by a categorical variable:

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  facet_wrap(~ factor(cyl))

Each panel shows cars with the same number of cylinders.

See Also