Skip to content

Instantly share code, notes, and snippets.

@karawoo
Created February 20, 2016 21:53
Show Gist options
  • Save karawoo/03de6b2cd048ed856a36 to your computer and use it in GitHub Desktop.
Save karawoo/03de6b2cd048ed856a36 to your computer and use it in GitHub Desktop.
Stacked area charts
library("ggplot2")
library("dplyr")
###########################################################
#### This does not work to create stacked area chart ####
###########################################################
## Create data
set.seed(40)
dat <- data.frame(x = rep(c(1:10), 3),
var = rep(c("a", "b", "c"), 10),
y = round(runif(30, 1, 5)))
## Plot
ggplot(dat, aes(x = x, y = y, fill = var)) +
geom_area(position = "stack")
###############################################
#### But it works if I re-order the rows ####
###############################################
dat %>%
arrange(var, x) %>%
ggplot(aes(x = x, y = y, fill = var)) +
geom_area(position = "stack")
@karawoo
Copy link
Author

karawoo commented Feb 20, 2016

unstacked_area

vs.

stacked_area

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment