How to customize titles, axis labels, etc. Expanded time series chart

I'm fairly familiar with the usual ways of modifying the plot by writing my own X-axis labels or main title, but I was unable to customize the output when plotting the time series decomposition results.

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 graph of the observed time series, trend, etc. With my own data (depth changes below the surface of the water), however, I would like to be able to toggle the y orientation (eg ylim = c (40,0) for "observables" or ylim = c (18,12) for "trend" ), changing from "seasonal" to "tidal" includes the units for the x-axis ("Time" (days) ') and provide a more detailed title for the figure.

My impression is that the time series analysis I am doing is pretty straightforward and in the end I might be better off using a different package, perhaps with better graphical control, but I would like to use ts () and decompose () if I can at the moment (yes, cake and consumption). Assuming it's not too bad.

Is there a way to do this?

Thank! Pete

+3


source to share


1 answer


You can change the function plot.decomposed.ts

(the plot

" method " that is dispatched at startup plot

for the class object decomposed.ts

(which is the class 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"), ...)
}

      

Note that in the code above, the function will hard-code the header. So let's change it so 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 an example of a ggplot plot. ggplot requires a dataframe, so the first step is to get the decomposed time series in the form of a dataframe and then print 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

+4


source







All Articles