Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get plot.ts to accept par or layout?

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?

like image 730
jay.sf Avatar asked Jan 21 '26 09:01

jay.sf


1 Answers

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

like image 138
Edward Avatar answered Jan 23 '26 18:01

Edward