I would like to create a proportional geom_area plot. I thought there must be the possibility to directly do this in ggplot2 instead of calculating the totals and the proportions beforehand. I created a minimal working example, which shows what I got now.
library(data.table)
library(dplyr)
library(zoo)
dt <- structure(list(Date = structure(c(2000, 2000, 2000, 2000, 2000,
2000.25, 2000.25, 2000.25, 2000.25, 2000.25, 2000.5, 2000.5,
2000.5, 2000.5, 2000.5, 2000.75, 2000.75, 2000.75, 2000.75, 2000.75,
2001, 2001, 2001, 2001, 2001, 2001.25, 2001.25, 2001.25, 2001.25,
2001.25, 2001.5, 2001.5, 2001.5, 2001.5, 2001.5, 2001.75, 2001.75,
2001.75, 2001.75, 2001.75, 2002, 2002, 2002, 2002, 2002, 2002.25,
2002.25, 2002.25, 2002.25, 2002.25, 2002.5, 2002.5, 2002.5, 2002.5,
2002.5, 2002.75, 2002.75, 2002.75, 2002.75, 2002.75), class = "yearqtr"),
Category = c(2L, NA, 1L, 4L, 3L, 2L, NA, 1L, 4L, 3L, 2L,
NA, 1L, 4L, 3L, 2L, NA, 1L, 4L, 3L, 2L, NA, 1L, 4L, 3L, 2L,
NA, 1L, 4L, 3L, 2L, 1L, 4L, NA, 3L, 2L, 1L, 4L, NA, 3L, 2L,
1L, 4L, NA, 3L, 2L, 1L, 4L, NA, 3L, 2L, 1L, 4L, NA, 3L, 2L,
1L, 4L, NA, 3L), Value = c(51, 15, 17, 3, 37, 50, 16, 17,
3, 47, 49, 16, 17, 3, 37, 53, 16, 17, 2, 38, 57, 2, 16, 2,
39, 58, 2, 16, 2, 39, 59, 17, 2, 2, 38, 59, 16, 3, 2, 37,
58, 17, 3, 3, 35, 58, 17, 3, 3, 36, 56, 17, 3, 3, 36, 57,
17, 3, 3, 37))
,.Names = c("Date", "Category", "Value")
, class = c("data.table", "data.frame"), row.names = c(NA, -60L))
data.table::melt(dt, id.vars = c("Date", "Category")
, measure.vars = c("Value")
) %>% ggplot(data = ., aes(x = Date, y = value, fill = as.factor(Category))) +
geom_area(stat = "identity") +
theme(legend.title=element_blank()) +
scale_x_yearqtr(format = "%Y-Q%q",n = 8, expand = c(0,0))
I would like to scale everything into the 0 to 1 space and let it fill it up completely.