Area Plots

Get introduced to area plots and learn how to generate them.

Feature

Area plots, also referred to as stacked area charts, are a form of data visualization that exhibit the evolution in the distribution of a whole over time. These plots share similarities with line charts but differ in that the space below the line is filled with color or shading to represent the data.

Press + to interact
Stacked area chart
Stacked area chart

We use area plots to compare the relative proportions of different categories or variables over time. For example, we could use an area plot to show the changing proportions of varying income levels in a population over several years.

Area charts with plot()

We use the polygon() function to create area charts in base R. We first create an empty chart and then add the layer created using the polygon() function. To create an empty chart, we use the type = 'n' argument in the plot() function.

The polygon() function takes two containers including a series of numbers. The numbers in the first variable are the x-coordinates, and the numbers in the second variable are the y-coordinates of the corner points. The function automatically creates an area by attaching the points in clockwise order. Then, the chart is created by painting the area.

plot(<central coordinate>, type = 'n', xlim = <range> , ylim = <range> )
polygon(<variable1>,<variable2>, col = <color>)

Let’s create a simple area chart.

Press + to interact
# We create a custom dataset in this exercise
var1 <- c(1,2,4,4) # Create the coordinate numbers for the x-axis
var2 <- c(3,5,6,4) # Create the coordinate numbers for the y-axis
plot(0, type = 'n', # Create an empty template
xlim = c(min(var1),max(var1)), # The range of the x-axis is defined
ylim = c(min(var2),max(var2))) # The range of the y-axis is defined
polygon(var1,var2, col = 'cyan') # Add the polygon layer to the template
points(var1,var2, pch = 15, lwd = 3 ) # Show the points on the chart
  • ...