ggsdc: Visualise seasonal decomposition

Description Usage Arguments Details Value See Also Examples

View source: R/ggsdc.R

Description

Creates a four-facet plot of seasonal decomposition showing observed, trend, seasonal and random components

Usage

1
2
3
4
ggsdc(data, mapping, frequency = NULL, method = c("stl", "decompose",
  "seas"), start = NULL, s.window, type = c("additive", "multiplicative"),
  index.ref = NULL, index.basis = 100, facet.titles = c("observed",
  "trend", "seasonal", "irregular"))

Arguments

data

dataset to use for plot.

mapping

List of aesthetic mappings. Must include x and y, and optionally can include colour/color

frequency

frequency of the period of the time series eg 12 = monthly

method

function to use for performing the seasonal decomposition. stl and decompose are functions in the stats package; seas is access to the seats program from X-13-SEATS-ARIMA via the seasonal package

start

starting time for the data; only needed if method = 'seas'.

s.window

parameter to pass to stl()

type

parameter to pass to decompose()

index.ref

if not NULL, a vector of integers indicating which elements of the beginning of each series to use as a reference point for converting to an index. If NULL, no conversion takes place and the data are presented on the original scale.

index.basis

if index.ref is not NULL, the basis point for converting to an index, most commonly 100 or 1000. See examples.

facet.titles

a vector in the order of observed, trend, seasonal and irregular for the titles of the four facets of the decomposition. Make sure you get the order right...

Details

This function takes a data frame and performs seasonal decomposition on the variable mapped to the y aesthetic, grouped by the variable (if any) mapped to the colour or color aesthetic. This allows the user to perform the equivalent of plot(stats::decompose(x)) but in the ggplot2 environment for themes, polishing etc; and to overlay decompositions on the same graphic; and with the X13-SEATS-ARIMA seasonal decomposition (so far only with default settings).

The "seasonal" component can be either multiplicative (in which case it will in a small range of values around one) or additive (in which case it will be on the scale of the original data), depending on the settings.

Value

an object of class ggplot with four facets

See Also

decompose, stl, seas

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# sample time series data in data frame
ap_df <- tsdf(AirPassengers)

ggsdc(ap_df, aes(x = x, y = y), method = "decompose") +
   geom_line()
   
ggsdc(ap_df, aes(x = x, y = y), method = "decompose", 
      type = "multiplicative") +
   geom_line(colour = "blue", size = 2) +
   theme_light(8)

ggsdc(ap_df, aes(x = x, y = y), method = "stl", s.window = 7) +
   labs(x = "", y = "Air passenger numbers") +
   geom_point()
   
## Not run:       
ggsdc(ldeaths_df, aes(x = YearMon, y = deaths, colour = sex), method = "seas") +
      geom_line()
      
serv <- subset(nzbop, Account == "Current account" & 
            Category %in% c("Services; Exports total", "Services; Imports total"))
ggsdc(serv, aes(x = TimePeriod, y = Value, colour = Category),
      method = "seas", start = c(1971, 2), frequency = 4) +
   geom_line()

## End(Not run)

ggsdc(ldeaths_df, aes(x = YearMon, y = deaths, colour = sex), s.window = 7, 
   index.ref = 1:12, index.basis = 1000) +
   geom_line() +
   ylab("Lung deaths index (average month in 1974 = 1000)")
      
bop <- subset(nzbop, Account == "Current account" & !Balance)
ggsdc(bop, aes(x = TimePeriod, y = Value, colour = Category), frequency = 4, 
   method = "decomp", type = "multiplicative") +
      geom_line() 
      
ggsdc(bop, aes(x = TimePeriod, y = Value, colour = Category), frequency = 4, s.window = 7) +
      geom_line() 

ggseas documentation built on June 12, 2018, 5:04 p.m.