Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show gaps in R/quantmod's chartSeries/candleChart plots

Tags:

r

quantmod

I am trying to show "gaps" in financial data using the plotting functions in the excellent quantmod package for R.

Normally R allows you to show gaps in plots using NA values, as with:

x<-1:10
y<-2*x
y[4:7]<-NA
plot(x,y,type="l")

I would like to do something similar with R/quantmod's candleChart plots. However, rows of data containing NA's are removed before plotting (there is a na.omit command in the chartSeries code that does this) so I cannot see how to do this.

An example is:

require(quantmod)

#Make some pretend data
x<-0:30
y<-100+20*sin(x)
y.open<-y[-length(y)]
y.close<-y[-1]
val<-as.xts(cbind(y.open,y.open+5,y.close-5,y.close,1000),order.by=as.POSIXct(paste("2011-01-",x[-1],sep='')))
colnames(val)<-c("Open","High","Low","Close","Volume")

#Plot this pretend data
candleChart(val,theme="white")

#Now try and make a "gap" in the middle of the data and plot it
val2<-val
val2[5:20,]<-NA
candleChart(val2,theme="white")

What is the "correct" way to do this? I guess I could overwrite chartSeries with my own version of this function (identical but without the na.omit() call), but that seems quite drastic.

Is there perhaps an option to do this kind of thing available? I have been unable to google anything useful...

Thanks, fttb

like image 520
fttb Avatar asked Apr 01 '11 15:04

fttb


1 Answers

The answer is not to use chartSeries, but rather the newer variant (still in development technically) chart_Series. Note the underscore.

chart_Series(val2)

If you're looking for more details on quantmod and using R in finance, we are hosting a large conference in Chicago at the end of this month. More info can be found here: R/Finance 2011

Hope that helps, and hope to see you in Chicago!!

like image 87
Jeff R Avatar answered Nov 15 '22 00:11

Jeff R