knitr::opts_chunk$set(echo = TRUE)
This package contains functions that allow users to plot decomposed time series data with ggplot2.
To install and load this package, you can use the following codes:
devtools:: install_github("brisneve/ggplottimeseries") library(ggplottimeseries)
Decompose time series data into its components (i.e. trends, seasonal, random) and collate them into a data frame.
To demonstrate this, we will use the function dts1 to decompose the daily sea surface temperature data into its three components. This dataset has 2142 observations on the following variables:
The dts1 function requires the following inputs:
Try the codes below and explore the output data frame which contains the components of the time series:
data(sst) x <- sst$date y <- sst$sst z <- 365.25 #number of days in a year df <- dts1(x,y,z, type = "additive") head(df)
Alternatively, if you have a time series-class data you can use the function dts2. See how this function works using the co2 dataset that contains a time series of atmospheric concentration of CO2. This function creates a dataframe of the time series components similar to that of dts1.
data(co2) df <- dts2(co2, type ="additive") head(df)
We now have a data frame that we can plot using ggplot. This package offers suite of functions to plot the decomposed time series data in ggplot and can be integrated with ggplot functions. We will use the co2 dataset to illustrate these functions.
Visualise the observed, trend, seasonal, and random components of time series into one figure
library(ggplot2) library(tidyr) #plots decomposed time series into one figure ggdecompose(df)+ xlab("Date")+ ylab("Atmospheric Concentration of CO2")
Visualise components of time series into separate figures
#plots the observations ggobserve(df)+ xlab("Date")+ ylab("Observed Atmospheric Concentration of CO2")
#plots the trend ggtrend(df)+ xlab("Date")+ ylab("Trend of Atmospheric Concentration of CO2")
#plots the seasonality ggseason(df)+ xlab("Date")+ ylab("Seasonality of Atmospheric Concentration of CO2")
#plots the random variation ggrandom(df)+ xlab("Date")+ ylab("Random Variation of Atmospheric Concentration of CO2")
Detrend and de-seasonalise timeseries and visualise the data
ggdetrend(df)+ xlab("Date")+ ylab("Detrended: Concentration of Atmospheric Concentration of CO2")
ggdeseason(df)+ xlab("Date")+ ylab("De-seasonalised: Concentration of Atmospheric Concentration of CO2")
Citation:
Brisneve Edullantes (2019). ggplottimeseries: Visualisation of Decomposed Time Series with ggplot2. R package version 0.1.0.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.