0

My plots with code:

set.seed(123)
x = arima.sim(n=300, model = list(ar=0.7, ma=0.5))

par(mfrow=c(1,2))
acf(x,main="Sample ACF")
pacf(x,main="Sample PACF")

returned output like this: enter image description here I tried to add following this and this answer, with code:

set.seed(123)
x = arima.sim(n=300, model = list(ar=0.7, ma=0.5))

par(oma=c(0,0,2,0),mfrow=c(1,2))
acf(x,main="Sample ACF")
pacf(x,main="Sample PACF")

also tried:

set.seed(123)
x = arima.sim(n=300, model = list(ar=0.7, ma=0.5))

par(par(xpd=NA),mfrow=c(1,2))
acf(x,main="Sample ACF")
pacf(x,main="Sample PACF")

but results didn't change.

nilsinelabore
  • 4,143
  • 17
  • 65
  • 122
  • 1
    The figure looks good with the code in the first block. Have you tried to run it in a clean R session? – mt1022 Apr 02 '21 at 08:30
  • Hi @mt1022 I was using R markdown. I tried to run it in a new R script and it seemed okay. Is there a way to do it in markdown? – nilsinelabore Apr 02 '21 at 08:36
  • 1
    I included the code in a markdown file and the figure in the resulting html file also looks fine. I guess you modified parameters of `par` somewhere else and forgot to restore it. Could you try `par(mfrow=c(1,2), mar = c(5, 4, 4, 2) + 0.1)`? – mt1022 Apr 02 '21 at 08:51
  • @mt1022 trying your code worked, thank you! But my plot remained the same. I don't quite understand how I modified the parameters, could you please tell me how I could restore the parameters? Please feel free to post your answer below – nilsinelabore Apr 02 '21 at 09:11
  • 2
    Any call to `par` (including `par=` as mentioned) is held constant for a particular graphics until that device is closed or something else changes it. For instance, run `par(mar=c(0,0,0,0))` and then plot anything. Then plot something completely different, no change. Now close the device (either its window or call `dev.off()`) and try a plot again without calling `par`. – r2evans Apr 02 '21 at 09:39
  • 1
    Except for calling `dev.off()` as mentioned by r2evans, you can also save the parameters before modification. Try this example `x <- par(mar = rep(0, 4)); plot(1:10); par(x); plot(1:10)`. `x` is a list that holds the parameters before the modification takes effects and calling `par(x)` restores those parameter values. – mt1022 Apr 02 '21 at 09:58
  • @r2evans Thank you for the explanation. This worked for me, though calling `dev.off()` at the beginning of the plot caught error `Error in dev.off() : cannot shut down device 1 (the null device)`. However with `dev.off`by removing the brackets it didn't seem to be a problem. Am I using it correctly? – nilsinelabore Apr 02 '21 at 10:40
  • It's a function, so just calling `dev.off` *does* very little. If it's just `mar` then you can reinstate the defaults with `par(mar=c(5,4,4,2)+0.1)`, without the need to close the device. My biggest concern (and why I didn't suggest that in the first place) is that it seems likely more than one parameter might have been changed, so I didn't want to reset just one. – r2evans Apr 02 '21 at 10:51
  • @r2evans So with consideration to the aforementioned error message, where should we add the `dev.off()`? – nilsinelabore Apr 02 '21 at 11:01
  • @r2evans Thanks I think it's working now – nilsinelabore Apr 02 '21 at 11:04

0 Answers0