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

会有一股神秘感。 提交于 2019-12-07 03:19:48

问题


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


回答1:


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!!



来源:https://stackoverflow.com/questions/5515295/how-to-show-gaps-in-r-quantmods-chartseries-candlechart-plots

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!