R/ggdecompose.R

Defines functions ggdecompose

Documented in ggdecompose

#' Visualisation of decomposed time series
#'
#' These functions plots the observed, trend, seasonal, and random components of time series
#' into one figure (ggdecompose) or into separate figures (ggobserve, ggtrend, ggseason, ggrandom,
#' respectively). These functions also plots detrended and deseasonalised time series
#' (ggdetrend and ggdeseason, respectively). These can be integrated with ggplot functions.
#'
#' @aliases
#' ggobserve
#' ggtrend
#' ggseason
#' ggrandom
#' ggdetrend
#' ggdeseason
#'
#' @usage
#' ggdecompose(x)
#' ggobserve(x)
#' ggtrend(x)
#' ggseason(x)
#' ggrandom(x)
#' ggdetrend(x)
#' ggdeseason(x)
#'
#' @param x a data frame generated by either dts or dts2 functions.
#'
#' @return This returns to a plot.
#'
#' @examples
#' data(co2)
#' x <- dts2(co2, type ="additive")
#' #plots decomposed time series into one figure
#' ggdecompose(x)+
#'  xlab("Date")+
#'  ylab("Atmospheric Concentration of CO2")

#' #plots components of time series into separate figure
#' ggobserve(x)+
#'  xlab("Date")+
#'  ylab("Observed Atmospheric Concentration of CO2")
#'
#' ggtrend(x)+
#'  xlab("Date")+
#'  ylab("Trend of Atmospheric Concentration of CO2")
#'
#' ggseason(x)+
#'  xlab("Date")+
#'  ylab("Seasonality of Atmospheric Concentration of CO2")
#'
#' ggrandom(x)+
#'  xlab("Date")+
#'  ylab("Random Variation of Atmospheric Concentration of CO2")
#'
#' #plots detrended and deseasonalised Time Series
#'
#' ggdetrend(x)
#'
#' ggdeseason(x)
#'
#' @author Brisneve Edullantes
#'
#' @export

ggdecompose <-function(x){
  if(!require(ggplot2)){install.packages("ggplot2"); library(ggplot2)}
  if(!require(tidyr)){install.packages("tidyr"); library(tidyr)}

  n <- tidyr:: gather(x, key = "components", value = "estimate", observation, trend, seasonal, random)
  n$components_f = factor(n$components, levels=c('observation','trend','seasonal','random'))
  ggplot(n,aes(x=date, y = estimate))+
    geom_line()+
    facet_grid(components_f~.,scales = "free_y")
}
brisneve/ggplottimeseries documentation built on May 7, 2019, 3:08 p.m.