3

I tried to use par with plot.ts but it didn't work. Examining the code I found, plot.ts uses already par internally, which may lead to a clash. However, also layout won't work. Here my code for both methods, the plots are plotted one after the other, but not side by side with both methods:

## using `par`
op <- par(mfrow=c(1, 2))
plot(stl(co2, s.window=21), plot.type="single")
plot(stl(log(co2), s.window=21), plot.type="single")
par(op)

## using `layout`
op <- par(no.readonly=TRUE)
layout(matrix(1:2, 1))
par(mfrow=c(1, 2))
plot(stl(co2, s.window=21), plot.type="single")
plot(stl(log(co2), s.window=21), plot.type="single")
par(op)

I am surprised that the question does not seem to have been asked earlier. How may I get the plot.ts function to accept par, layout, or any hack to plot side by side?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Not sure, if you are open to ggplot, but there is a pkg called patchwork which has a nice interface to work with ggplots and a nice syntax to arrange plots. Simply you can write P1+P2 to arrange two plots side by side. – monte Jun 07 '20 at 06:33
  • @MohitSharma I'm talking about the `stats::plot.ts` function, thanks. – jay.sf Jun 07 '20 at 06:39
  • 1
    Actually, you're calling `plot.stl`, which also modifies the `par` settings internally. I think you'll have to utilize a hack. – Edward Jun 07 '20 at 08:36

1 Answers1

1

You should be able to do this with a little hack of the plot.stl function from the stats package.

Just comment out lines 15:19

#15:  if (length(set.pars)) {
#16:    oldpar <- do.call("par", as.list(names(set.pars)))
#17:    on.exit(par(oldpar), add = TRUE)
#18:    do.call("par", set.pars)
#19:  }

Then using layout with an 8x2 matrix:

layout(matrix(1:8, ncol=2, byrow=FALSE))
layout.show(8)
op <- par(mar = c(0, 4, 0, 4), oma = c(2, 0, 2, 0), tck = -0.02)
plot(stl(co2, s.window=21), plot.type="single")
plot(stl(log(co2), s.window=21), plot.type="single")
par(op)

enter image description here

Edward
  • 10,360
  • 2
  • 11
  • 26