Area chart with base R

This post describes how to build an area chart using base R and the polygon() function. See the area chart section for a ggplot2 implementation.

Area chart Section About area chart

Basic area chart with plot() and polygon()

Base R also allows to build area charts thanks to the polygon() function. This function requires 2 inputs: x and y.

Note that extreme values of both are added at the beginning and at the end of each vectors to make sure the polygon is closed.

# Create data
data <- data.frame(x=seq(1,10), y=sample(seq(1,15),10))

# Draw line on top
plot(data, col=rgb(0.2,0.1,0.5,0.9), type="o", lwd=3, xlab="", ylab="size",

# Fill the area
polygon(c(min(data$x), data$x, max(data$x)),
        c(min(data$y), data$y, min(data$y)),
        col=rgb(0.2,0.1,0.5,0.2), border=FALSE)

Related chart types

Line plot
Stacked area
Time Series

❤️ 10 best R tricks ❤️

👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥