How to create a stacked line plot

BurninLeo picture BurninLeo · Nov 30, 2012 · Viewed 21.4k times · Source

There are multiple solutions to create a stacked bar plot in R, but how to draw a stacked line plot?

enter image description here

Answer

Sven Hohenstein picture Sven Hohenstein · Nov 30, 2012

A stacked line plot can be created with the ggplot2 package.

Some example data:

set.seed(11)
df <- data.frame(a = rlnorm(30), b = 1:10, c = rep(LETTERS[1:3], each = 10))

The function for this kind of plot is geom_area:

library(ggplot2)
ggplot(df, aes(x = b, y = a, fill = c)) + geom_area(position = 'stack')

enter image description here