R/plot_AnimatedBetaEst.R

Defines functions plot_AnimatedBetaEst

Documented in plot_AnimatedBetaEst

#' Animated beta distribution estimates evolution
#' @author Simon P Castillo \email{spcastil@@uc.cl}.
#' @description This function plots the animated temporal evolution of the estimated parameters of beta distribution $\hat{\alpha}$ and $\hat{\beta}$
#' @param CPAD: dataframe. The dataframe returned by \code{\link{analyse_CPAD}}.
#' @param saveAnim: \code{TRUE} or \code{FALSE}. Save the animation.Default \code{FALSE}.
#' @return This function returns to your Viewer panel the animated temporal evolution of $\hat{\alpha}$ and $\hat{\beta}$ .
#' Also, if \code{saveAnim} is \code{TRUE}, a folder named \code{GIFs} is created in your \code{wd} with the animation in \code{gif} format.
#' @examples plot_AnimatedBetaEst(CPAD, logScale=TRUE, saveAnim=FALSE)
#'
#'
#'
plot_AnimatedBetaEst <- function(CPAD, saveAnim=FALSE){

  pacman::p_load(ggplot2,lubridate, viridis, rlist, patchwork, gganimate)

  df4 = CPAD
  breaksdate = seq(range(CPAD$day)[1],range(CPAD$day)[2], length.out = 5) #c(min(df2b$date),as.Date("2020-02-28"),as.Date("2020-02-29"),as.Date("2020-04-14"),max(df2b$date))#


  space = ggplot(df4, aes(log(alfa), log(beta),colour=as.numeric(day),
                         label = datetext)) +
    geom_point(aes(group = seq_along(day)), size=2)+
    geom_text(aes(-2.2, 3.5), size = 12, hjust = 0, color = "gray70") +
    geom_path(size=1.5, alpha=0.3)+
    theme_minimal() +
    guides(colour=FALSE)+
    theme(axis.title=element_text(size=12,face="bold"))+
    scale_colour_viridis_c(breaks = as.numeric(seq(range(df4$day)[1],range(df4$day)[2], length.out = 5)),
                           labels = paste0(day(seq(range(df4$day)[1],range(df4$day)[2], length.out = 5)), "-", month(seq(range(df4$day)[1],range(df4$day)[2], length.out = 5), label = TRUE)),
                           name = "Day",
                           option = "plasma") +
    labs(x= expression(paste("ln(",alpha, ")")), y= expression(paste("ln(",beta, ")"))) +
    transition_reveal(as.Date(df4$datetext, "%d %b"))+
    enter_fade()+
    exit_fade()


  #space
  #space2=animate(space, nframes = length(unique(CPAD$day))*3)
  animate(space, nframes = length(unique(CPAD$day))*3)
  if(saveAnim==TRUE){
  dir.create("GIFs")
  anim_save(animation = animate(space, nframes = length(unique(CPAD$day))*2), filename = "GIFs/plot_AnimatedBeta.gif")
  }

}
simonpcastillo/CPAD documentation built on Dec. 31, 2020, 7:27 a.m.