1

I am using xtsExtra to plot some scatter graphs of xts objects and wanted to draw a horizontal line through zero but it looks like its in the wrong place...please see the code below? is this a bug?

set.seed(123)
require(xts)
f <- xts(rnorm(100,0.0001,0.003),Sys.Date()-c(100:1))
f1 <- xts(rnorm(100,0.0001,0.003),Sys.Date()-c(100:1))
require(xtsExtra)
plot(f,f1)
abline(h=0,col='red')
h.l.m
  • 13,015
  • 22
  • 82
  • 169
  • maybe, but why would you use xts objects like that? plot.xts expects the x-axis to be timeBased. – GSee Sep 20 '13 at 18:59
  • ok...even if you did `plot(f)` to get an x-asis time base then `abline(h=0, col='red')`, it's still wrong... – h.l.m Sep 20 '13 at 19:22
  • I share GSee's views. Vote to close as unconstructive unless questioner can come up with a meaningful reason for in effect using an object called "eXtensible Time Series" *without* a time series. – SlowLearner Sep 20 '13 at 21:55
  • When I try `plot(f); abline(h=0)` the line appears in the correct location, so exactly what is "wrong"? It's also not clear whether you are trying to do a scatterplot of the coredata values or want a "parallel-plot" of two time indexed series. – IRTFM Sep 21 '13 at 15:30
  • 2
    @SlowLearner How is this unconstructive? One draws an `xts` plot and then wants to `abline` on it. For example I `quantmod::getSymbols('GDP',src='FRED')` and want to shade recessions....it's a common use case. – isomorphismes Jul 29 '14 at 17:52

1 Answers1

3

Jeff Ryan may have answered this here:

Try,

abline(v=.index(X)[50], col="red")

Note the "dot" in .index. The fact is that xts uses the internal POSIXct(like) time for the x-axis, so you need to access it that way.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
isomorphismes
  • 8,233
  • 9
  • 59
  • 70
  • Thanks for that comment, but it doesn't solve the horizontal line case...as the `v` is for vertical...also in my example, the x-axis is not time... – h.l.m Jul 29 '14 at 21:29
  • @h.l.m ok. Using `quantmod::getSymbols('GDP',src='FRED')` I had no problem with `abline(h=0)`, only with the `v`. – isomorphismes Jul 30 '14 at 07:11
  • @h.l.m With your example I was able to `abline(h=.5)` and get a line in the middle. – isomorphismes Jul 30 '14 at 07:26
  • I think that the values that `v` and `h` used are based on a new scale...that is used by `plot.xts`, just not too sure how to access and efficiently use that scale so that it is intuitive... – h.l.m Jul 30 '14 at 07:42
  • This is great! I ended up with the following code: abline(v=index(my.xts.data)[endpoints(my.xts.data, "days")]). In this way R makes grid lines for daily time steps. – Johanna Sörensen Jan 16 '20 at 23:01