I have data in which two types of occurrences are registered: type_a and type_b and their year of occurrence.
This is one way to generate an example of my data:
set.seed(1)
years <- 1991:2010
type_a_years <- 20
type_b_years <- 10
type_a <- round(runif(type_a_years, 0, 5))
type_b <- c(rep(0, type_a_years-type_b_years),round(runif(type_b_years, 5, 7)))
df <- data.frame(year = unlist(sapply(1:length(years), function(x) c(rep(years[x], type_a[x]),rep(years[x], type_b[x])))),
type = unlist(sapply(1:length(years), function(x) c(rep("type_a", type_a[x]),rep("type_b", type_b[x])))))
head(df)
year type
1 1991 type_a
2 1992 type_a
3 1992 type_a
4 1993 type_a
5 1993 type_a
6 1993 type_a
I'd like to generate a cumulative frequency distribution plot of type_b events stacked on top of type_a events by year and I'd like the distributions to appear as curves rather than bars.
I'm guessing it should be some manipulation of:
library(ggplot2)
ggplot(df, aes(year)) + stat_ecdf()
Where I'll get two curves and stacked by type where the area under each type will be filled with a different color. That is, the area between the type_a curve and the x-axis will be in one color and the area between the type_b curve and the type_a curve will be in another color.