1

The standard plot method immediately displays a result. But with xts objects, this only works when plot.xts is not called within a loop. For example, this code works correctly:

library(xts)
data(sample_matrix)
sample.xts <- as.xts(sample_matrix)
plot(sample.xts)

Whereas the following code does not display any result:

# dev.off()
par(mfrow=c(1,2))
for (i in seq(2)) {
    plot(sample.xts)
}

Where is the plot in the second case? And why xts.plot does not act like the standard plot function?

lkegel
  • 193
  • 9
  • 1
    Check this [out](http://stackoverflow.com/questions/12992831/plotting-multple-xts-objects-in-one-window) – Chirayu Chamoli Oct 04 '16 at 12:21
  • Thank you for the pointer, @ChirayuChamoli ! Indeed, coercing to `zoo` helps in this case. – lkegel Oct 04 '16 at 12:33
  • 1
    it works as expected, not reproducible – Sandipan Dey Oct 04 '16 at 12:57
  • @sandipan you mean, the second code snippet works correctly? Why is the plot not displayed? I'm using RGui and Eclipse + StatET plugin. – lkegel Oct 04 '16 at 13:04
  • @LarsK., yes for me it displays the two plots side be side as expected, using RStudio – Sandipan Dey Oct 04 '16 at 13:07
  • OK, apparently the version I am using (0.10-0, https://github.com/joshuaulrich/xts) behaves differently than the official version 0.9-7 from cran. I installed the latter one and it works in all of the mentioned IDEs. – lkegel Oct 04 '16 at 13:42

1 Answers1

8

Plot returns a plot object, which in your first case gets printed by default. In a loop, or function, you need to explicitly print it.

par(mfrow=c(1,2))
for (i in seq(2)) {
    print(plot(sample.xts))
}
Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • Thank you for this remark! As I mentioned earlier, I was using version 0.10-0 from github that explicitly needs a print statement. In version 0.9-7, this was not necessary. – lkegel Oct 04 '16 at 13:48
  • I needed it with xts version 0.9.874, from within RStudio. – Darren Cook Oct 04 '16 at 14:10