R was built by statisticians for statisticians, so plotting is baked right in. With just plot() you can create a scatter plot, line chart, or both. Add a few arguments to tweak colors, titles, and axis labels โ and suddenly your data tells a story.
Scatter Plots with plot()
The workhorse is plot(). Give it x and y values and you get a scatter plot. The type argument controls whether you see points ("p"), lines ("l"), or both ("b").
x <- 1:10
y <- c(2, 5, 3, 8, 7, 9, 6, 11, 10, 13)
plot(x, y, main = "Sales Over Time",
xlab = "Month", ylab = "Sales",
col = "blue", type = "b")
Try it Yourself โ
Customizing Your Plot
R lets you style your plot with extra arguments: col for color, main for the title, xlab and ylab for axis labels, pch for point symbols, and lwd for line width. Mix and match to make your chart pop.
x <- 1:5
y <- c(3, 7, 2, 9, 4)
plot(x, y,
main = "Customized Plot",
xlab = "X Axis", ylab = "Y Axis",
col = "red", pch = 19, type = "o",
lwd = 2)
Try it Yourself โ
Multiple Series on One Plot
Use lines() and points() to add extra data to an existing plot. They share the same coordinate space, so you can overlay trends or highlight specific points.
x <- 1:10
y1 <- x + rnorm(10, mean = 0, sd = 1)
y2 <- x * 2 + rnorm(10, mean = 0, sd = 1)
plot(x, y1, type = "p", col = "blue",
main = "Two Series", xlab = "X", ylab = "Y")
lines(x, y2, col = "red", type = "o")
Try it Yourself โ