R Graphics · Area Chart

Showing Magnitude over Time with Area Charts in R

Create single, stacked, proportional, stepped, uncertainty, and ggplot2 area displays.

6 examplesReproducible R code
Before you begin

Choose the variation that serves the analysis

Area charts emphasize accumulated magnitude across an ordered axis. They work best with a meaningful baseline and a small number of clearly distinguishable series.

Examples
06
Levels
Beginner + Intermediate
01
Example 01Base RBeginner

Create a Basic Area Chart

A filled area chart emphasizes the magnitude of a series relative to a baseline.

Create a Basic Area Chart example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x <- 1:24; y <- cumsum(rnorm(24, .3, .5)) + 10
plot(x, y, type="n", xlab="Period", ylab="Value")
polygon(c(x, rev(x)), c(y, rep(0, length(y))), col="#bfdbfe", border=NA)
lines(x, y, col="#2563eb", lwd=3)
02
Example 02Base RBeginner

Stack Multiple Areas

Stacked areas show how several components contribute to a changing total.

Stack Multiple Areas example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x <- 1:20; a <- 3 + sin(x/3); b <- 2 + cos(x/4); c <- 1 + .05*x
y <- rbind(a, a+b, a+b+c)
matplot(x, t(y), type="n", xlab="Period", ylab="Total")
polygon(c(x,rev(x)),c(y[3,],rep(0,20)),col="#dbeafe",border=NA)
polygon(c(x,rev(x)),c(y[2,],rep(0,20)),col="#60a5fa",border=NA)
polygon(c(x,rev(x)),c(y[1,],rep(0,20)),col="#1d4ed8",border=NA)
03
Example 03Base RBeginner

Build a 100% Stacked Area Chart

A proportional area chart compares composition while holding the total height constant.

Build a 100% Stacked Area Chart example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x <- 1:20; parts <- rbind(2+sin(x/3), 2+cos(x/4), 1+.05*x)
shares <- apply(parts, 2, function(z) cumsum(z/sum(z)))
plot(x, shares[3,], type="n", ylim=c(0,1), ylab="Share")
for(i in 3:1) polygon(c(x,rev(x)),c(shares[i,],rep(0,20)), col=c("#1d4ed8","#60a5fa","#dbeafe")[i], border=NA)
04
Example 04Base RIntermediate

Use a Stepped Area

A stepped area is appropriate when values remain constant until the next recorded change.

Use a Stepped Area example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x <- 1:12; y <- c(4,4,5,5,7,7,6,8,8,9,9,10)
plot(x,y,type="s",lwd=3,col="#2563eb",ylim=c(0,11))
polygon(c(x,rev(x)),c(y,rep(0,12)),col=adjustcolor("#2563eb",.2),border=NA)
05
Example 05Base RIntermediate

Display an Uncertainty Area

A ribbon-shaped area communicates a range around an estimated series.

Display an Uncertainty Area example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
x<-1:30; fit<-10+.2*x+sin(x/4); se<-.8+.01*x
plot(x,fit,type="n",ylim=range(fit-se,fit+se))
polygon(c(x,rev(x)),c(fit-se,rev(fit+se)),col=adjustcolor("#2563eb",.2),border=NA)
lines(x,fit,col="#2563eb",lwd=3)
06
Example 06ggplot2Intermediate

Create an Area Chart with ggplot2

geom_area() provides a layered grammar for filled time-series displays.

Create an Area Chart with ggplot2 example generated in R
R-generated example output.

Run the code, inspect how the visual encoding changes, and adapt the labels, scales, and styling to the analytical question rather than treating the defaults as fixed.

RRun this code
library(ggplot2)
data <- data.frame(period=1:24, value=cumsum(rnorm(24,.3,.5))+10)
ggplot(data,aes(period,value))+geom_area(fill="#60a5fa",alpha=.6)+geom_line(color="#1d4ed8")+theme_minimal()