#' Plot aggregate compartment trajectories
#'
#' Convenience plot of the five aggregate compartment trajectories
#' produced by a SEAIR solution.
#'
#' @param sol A \code{"seair_solution"} object.
#' @param which Character vector of compartments to plot; any subset
#'   of \code{c("S","E","A","I","R")}.
#' @param ... Additional graphical parameters passed to
#'   \code{\link[graphics]{matplot}}.
#'
#' @return Invisibly returns the aggregate data frame.
#'
#' @examples
#' m <- seq(0, 1, length.out = 31)
#' f <- dbeta(m, 2, 2)
#' init <- seair_init(m, f, I_seed = 1e-4)
#' pars <- seair_params(beta = 1.5, sigma = 0.3, kappa = 0.2,
#'                      gamma_A = 0.1, gamma_I = 0.13,
#'                      alpha = 0.5, delta = 0.3)
#' sol <- seair_solve(init, pars, times = seq(0, 30, by = 2))
#' plot_seair(sol, which = c("S","I","R"))
#'
#' @export
plot_seair <- function(sol,
                       which = c("S", "E", "A", "I", "R"),
                       ...) {
  stopifnot(inherits(sol, "seair_solution"))
  which <- match.arg(which, c("S", "E", "A", "I", "R"),
                     several.ok = TRUE)
  agg <- seair_aggregate(sol)
  Y <- as.matrix(agg[, which, drop = FALSE])
  graphics::matplot(agg$time, Y, type = "l", lty = 1,
                    xlab = "time", ylab = "population fraction",
                    ...)
  graphics::legend("topright", legend = which, lty = 1,
                   col = seq_along(which), bty = "n")
  invisible(agg)
}
