-
Notifications
You must be signed in to change notification settings - Fork 60
/
geom_bands.R
42 lines (42 loc) · 1.55 KB
/
geom_bands.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#' @title ggplot2 Geom for Quantile Bands
#'
#' @description Plots quantile bands given a data.frame with stochastic model
#' results from \code{\link{icm}} or \code{\link{netsim}}.
#'
#' @param mapping Standard aesthetic mapping \code{aes()} input for ggplot2.
#' @param lower Lower quantile for the time series.
#' @param upper Upper quantile for the time series.
#' @param alpha Transparency of the ribbon fill.
#' @param ... Additional arguments passed to \code{stat_summary}.
#'
#' @details
#' This is a wrapper around \code{ggplot::stat_summary} with a ribbon geom as
#' aesthetic output.
#'
#' @export
#' @keywords plot
#'
#' @examples
#' param <- param.icm(inf.prob = 0.2, act.rate = 0.25)
#' init <- init.icm(s.num = 500, i.num = 1)
#' control <- control.icm(type = "SI", nsteps = 250, nsims = 5)
#' mod1 <- icm(param, init, control)
#' df <- as.data.frame(mod1)
#' df.mean <- as.data.frame(mod1, out = "mean")
#'
#' library(ggplot2)
#' ggplot() +
#' geom_line(data = df, mapping = aes(time, i.num, group = sim),
#' alpha = 0.25, lwd = 0.25, color = "firebrick") +
#' geom_bands(data = df, mapping = aes(time, i.num),
#' lower = 0.1, upper = 0.9, fill = "firebrick") +
#' geom_line(data = df.mean, mapping = aes(time, i.num)) +
#' theme_minimal()
#'
geom_bands <- function(mapping, lower = 0.25, upper = 0.75, alpha = 0.25, ...) {
stat_summary(mapping,
geom = "ribbon",
fun.min = function(x) quantile(x, lower),
fun.max = function(x) quantile(x, upper),
alpha = alpha, ...)
}