Seasonality analysis is one of the core elements of the descriptive analysis process of time series data. This process is typically done with the use of data visualization tools and summary statistics methods. The **TSstudio** package provides a set of functions for seasonality plots.

The ts_decompose function is an interactive wrapper for the `decompose`

function from the **stats** package. It provides a decomposition of the series into seasonal, trend, and irregular components. The decomposition of the series is done by using a moving average. The following example demonstrated the decomposition of the US monthly consumption of natural gas (`USgas`

):

The `ts_seasonal`

function provides three different seasonal views of the series for low-frequency time series objects (i.e., monthly, quarterly, half-year). The type of seasonal view defined by the `type`

argument. The default view, `type = normal`

, split and plot the series by its full cycle units (which is typically years). This allows comparing the changes of the series from year to year by the series frequency units (e.g. the month of the year), and therefore, to identify seasonal patterns:

`ts_seasonal(USgas, type = "normal")`

Note that the color scale of the series cycles lines (which in the case above represents the years) is set by chronological order. The second view, `type = cycle`

, split and plot the series by its frequency units over time. This enables to identify if there is a hierarchy relationship between the frequency units of the series, or how strong the seasonal pattern of the series:

`ts_seasonal(USgas, type = "cycle")`

In the plot above, you can notice that while the series is trending up, the hierarchy between the different months kept over time (e.g., typically, the peak of the consumption occurs during January). The third and last view, `type = box`

, returns a box-plot of each frequency unit:

`ts_seasonal(USgas, type = "box")`

As each view may reveal different patterns, it would make more sense to plot all the three together. This can be done by setting `type = all`

:

`ts_seasonal(USgas, type = "all")`

Note that some of the variations seen in those seasonal plots, especially in the `box`

view, related to the series trend. In some cases, it would make more sense to remove the trend from the series and replot it. The next example demonstrated the plot of the series without the trend. A simple detrending of the series is done with the decompose function:

```
USgas_detrend <- USgas - decompose(USgas)$trend
ts_seasonal(USgas_detrend, type = "all")
```

Another way to look at seasonality is with the use of heatmap. The ts_heatmap function returns heatmap of the series object:

`ts_heatmap(USgas)`

The `color`

argument defines the heatmap color scale supporting the RColorBrewer colors palettes. For example, we can modify the heatmap above to red scale by using the `Reds`

colors palette:

`ts_heatmap(USgas, color = "Reds")`

The `ts_surface`

function provides a 3D plot for low-frequency time-series data (e.g., monthly), by plotting the cycles (years), frequency units (months) and the corresponding values of the series:

`ts_surface(USgas)`

The `ts_polar`

function returns a polar plot representation for monthly and quarterly time series data:

`ts_polar(USgas)`

High-frequency time-series data (e.g., daily, hourly, minutely, etc.) may have more than one seasonal patterns. For example, hourly data may have several seasonal layers such as the hour of the day, day of the week and month of the year. A good example for such type of series is the demand for electricity in the UK that available on the **UKgrid** package:

```
library(UKgrid)
UKgrid_df <- extract_grid(type = "data.frame",
columns = "ND",
start = 2017,
aggregate = "hourly")
str(UKgrid_df)
#> 'data.frame': 24264 obs. of 2 variables:
#> $ TIMESTAMP: POSIXct, format: "2017-01-01 00:00:00" "2017-01-01 01:00:00" ...
#> $ ND : int 55216 54044 50183 46724 44389 43533 44141 45165 47991 53183 ...
```

As can be seen in the plot below, the series has strong seasonality across the year (high consumption during the wintertime and low throughout the summer). If zooming in (use the slider), you can reveal more seasonal patterns during the week and day time:

The `ts_quantile`

function provides a quantile plot for time series object (supporting `zoo`

, `xts`

, `data.frame`

, `data.table`

and `tbl`

objects). For example, let’s plot the quantile plot of the demand for electricity:

`ts_quantile(UKgrid_df)`

By default, the function will calculate and plot the quantile of the series over the base frequency of the series, which in this case, is 24 hours. The `period`

argument enables to plot the quantile of the series base frequency condition on different periodicities such as weekdays or month. For example, setting the `period`

argument to `weekdays`

, enable to view the variation of the series during the 24-hour cycle condition on the day of the week:

`ts_quantile(UKgrid_df, period = "weekdays", n = 2)`

Similarly, when setting the `period`

argument to `monthly`

, the function returns the 24-hour cycle condition on the month of the year:

`ts_quantile(UKgrid_df, period = "monthly", n = 2)`

On a side note, you should be aware that some of the variations of the quantile plot may result from the growth of the series over time (or the series trend). Therefore, to reduce the trend effect from the plot either detrend the series or select a short period (such as one or two full cycles)