Adding a vertical line to a chartSeries chart
Hopefully this is not redundant as I searched a lot and couldn't find an answer. I am drawing intraday data and want to place a vertical line at a specific point in time. I seem to need to use the addTA function, but it always displays below my graph in some strange empty white space. Here is some sample code and data. Thanks for any help.
Data:
date,value
29-DEC-2010:00:02:04.000,99.75
29-DEC-2010:00:03:44.000,99.7578125
29-DEC-2010:00:05:04.000,99.7578125
29-DEC-2010:00:07:53.000,99.7421875
29-DEC-2010:00:07:57.000,99.71875
29-DEC-2010:00:09:20.000,99.7421875
29-DEC-2010:00:11:04.000,99.75
29-DEC-2010:00:12:56.000,99.7421875
29-DEC-2010:00:13:05.000,99.7421875
code:
#set up data
data = read.csv("foo.csv")
values = data[,2]
time = c(strptime(data[,1],format="%d-%b-%Y:%H:%M:%S",tz="GMT"))
dataxts = xts(values, order.by=time,tzone="GMT")
# chart data
chartSeries(dataxts)
# add vertical line - this is where I have no clue what going on.
addTA(xts(TRUE,as.POSIXlt("2010-12-29 00:11:00",tz="GMT"),on=1))
What ultimately happens is that I get a vertical line where I want, 2010-12-29 00:11:00, but it's in a new section below the graph instead of overlaying it. Any ideas?
source to share
You are passing on
as an argument xts
, but you must pass it addTA
.
I think you want to do this:
addTA(xts(TRUE,as.POSIXlt("2010-12-29 00:11:00",tz="GMT")),on=1)
However, this still doesn't work for me with your example data. However, if you have a job with your real data, except that it puts a row in a new pane, then this should work and not open a new pane.
I have a addVLine
function in my qmao package that is essentially the same thing.
Edit
Aside from the parenthesis typo, the xts object also requires a column name in order to addTA
work (I think ... at least in this case anyway). Also, you must specify the x value that exists in your graph (i.e. 11:04, not 11:00)
colnames(dataxts) <- "x"
chartSeries(dataxts)
addTA(xts(TRUE,as.POSIXlt("2010-12-29 00:11:04",tz="GMT")),on=1, col='blue')
# or
# library(qmao)
# addVLine(index(dataxts[7]))
Edit 2
Perhaps the best approach is to use the function addLines
addLines(v=7)
Or, if you know the time but don't know the line number, you can do this
addLines(v=which(index(dataxts) == as.POSIXlt("2010-12-29 00:11:04", tz="GMT")))
which gives
source to share