Learn R Programming  /  Base Graphics  /  plot() Basics

plot() Basics

plot() is base R's all-purpose graphing function. Its behaviour adapts to what you give it, making it the fastest way to take a first visual look at data.

A Basic Scatter Plot

R Code
x <- c(1,2,3,4,5)
y <- c(2,4,5,4,6)

plot(x, y)
Output
(a scatter plot window opens, showing 5 points)

Common Styling Arguments

R Code
plot(x, y,
     main = "My First Plot",  # chart title
     xlab = "X values",       # x-axis label
     ylab = "Y values",       # y-axis label
     col  = "steelblue",       # point/line colour
     pch  = 19,               # point shape (19 = solid circle)
     type = "b")               # "b" = both points and connecting lines

The type Argument

  • "p" — points only (default)
  • "l" — lines only
  • "b" — both points and lines
  • "h" — vertical bars from zero to each point (like a lollipop chart)
  • "s" — a step function

Adding Extra Elements to an Existing Plot

R Code
plot(x, y, pch = 19)
abline(h = 4, col = "red", lty = 2)   # horizontal reference line
lines(x, y + 1, col = "darkgreen")         # add a second line
legend("topleft", legend = c("series 1", "series 2"),
       col = c("black","darkgreen"), pch = 19)

Advanced: Multiple Plots in One Window

🚀 Advanced Example
par(mfrow = c(rows, cols)) arranges several plots into a grid in a single graphics window - useful for comparing distributions side by side.
R Code
par(mfrow = c(1, 2)) # 1 row, 2 columns
plot(x, y, main = "Plot A")
plot(x, y^2, main = "Plot B")
par(mfrow = c(1, 1)) # reset back to a single plot

Key Points

  • plot() adapts its behaviour to whatever data type it receives.
  • main, xlab, ylab, col, and pch are the most common styling arguments.
  • abline(), lines(), points(), and legend() add extra elements to an already-open plot.
  • par(mfrow = c(rows, cols)) arranges multiple plots in a grid.