Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize title, axis labels, etc. in a plot of a decomposed time series

I'm reasonably familiar with the usual ways of modifying a plot by writing your own x axis labels or a main title, but I've been unable to customize the output when plotting the results of a time series decomposition.

For example,

library(TTR)
t <- ts(co2, frequency=12, start=1, deltat=1/12)
td <- decompose(t)
plot(td)
plot(td, main="Title Doesn't Work") # gets you an error message

gives you a nice, basic plot of the observed time series, trend, etc. With my own data (changes in depth below the water surface), however, I'd like to be able to switch the orientation of the y axes (eg ylim=c(40,0) for 'observed', or ylim=c(18,12) for 'trend'), change 'seasonal' to 'tidal', include the units for the x axis ('Time (days)'), and provide a more descriptive title for the figure.

My impression is that the kind of time series analyses I'm doing is pretty basic and, eventually, I may be better off using another package, perhaps with better graphical control, but I'd like to use ts() and decompose() if I can for now (yeah, cake and consumption). Assuming this doesn't get too horrendous.

Is there a way to do this?

Thanks! Pete

like image 383
Peter Nelson Avatar asked Mar 27 '17 19:03

Peter Nelson


1 Answers

You can modify the plot.decomposed.ts function (that's the plot "method" that gets dispatched when you run plot on an object of class decomposed.ts (which is the class of td).

getAnywhere(plot.decomposed.ts)
function (x, ...) 
{
    xx <- x$x
    if (is.null(xx)) 
        xx <- with(x, if (type == "additive") 
            random + trend + seasonal
        else random * trend * seasonal)
    plot(cbind(observed = xx, trend = x$trend, seasonal = x$seasonal, random = x$random), 
         main = paste("Decomposition of", x$type, "time series"), ...)
}

Notice in the code above that the function hard-codes the title. So let's modify it so that we can choose our own title:

my_plot.decomposed.ts = function(x, title="", ...) {
  xx <- x$x
  if (is.null(xx)) 
    xx <- with(x, if (type == "additive") 
      random + trend + seasonal
      else random * trend * seasonal)
  plot(cbind(observed = xx, trend = x$trend, seasonal = x$seasonal, random = x$random), 
       main=title, ...)
}

my_plot.decomposed.ts(td, "My Title")

enter image description here

Here's a ggplot version of the plot. ggplot requires a data frame, so the first step is to get the decomposed time series into data frame form and then plot it.

library(tidyverse) # Includes the packages ggplot2 and tidyr, which we use below

# Get the time values for the time series
Time = attributes(co2)[[1]]
Time = seq(Time[1],Time[2], length.out=(Time[2]-Time[1])*Time[3])

# Convert td to data frame
dat = cbind(Time, with(td, data.frame(Observed=x, Trend=trend, Seasonal=seasonal, Random=random)))

ggplot(gather(dat, component, value, -Time), aes(Time, value)) +
  facet_grid(component ~ ., scales="free_y") +
  geom_line() +
  theme_bw() +
  labs(y=expression(CO[2]~(ppm)), x="Year") +
  ggtitle(expression(Decomposed~CO[2]~Time~Series)) +
  theme(plot.title=element_text(hjust=0.5))

enter image description here

like image 190
eipi10 Avatar answered Sep 29 '22 03:09

eipi10