R/tsplot.R

#' @importFrom ggplot2 ggplot geom_point geom_line aes labs
#' @importFrom magrittr `%>%`
#' @importFrom stats time

NULL


#' Plot solution to initial value problem
#'
#' Use time series dataframe created my mmbr::solve() to produce a time series
#' plot.
#'
#' @param params object of class "LotkaVolterraParams"
#' @param time_series dataframe
#'
#' @return plot
#' @export
tsplot <- function (params, time_series) {

  # Format data for ggplot2.
  formatted <- data.frame(time_series) %>%
    tidyr::gather(species, population, prey_population, predator_population) %>%
    dplyr::mutate(species = dplyr::if_else(species == 'prey_population', 'Prey', 'Predator'))

  plot <- ggplot(formatted, mapping = aes(x = time, y = population)) +
    geom_line(aes(color = species)) +
    geom_point(aes(color = species)) +
    labs(
      title = 'Lotka-Volterra predator prey model',
      color = 'Species',
      x = 'Time', y = 'Population size'
    )

  return (plot)
}
atmunr/mmbr documentation built on Oct. 6, 2020, 2:51 a.m.