code.R
require(rCharts)
#nvd3 rCharts do not show up in Rstudio viewer
#so turn viewer off
options(viewer=NULL)
require(quantmod)
getSymbols(c("^VIX","^GSPC"),from="2012-01-01")
roc <- ROC(
na.omit(merge(VIX[,4],GSPC[,4])),
n=1,
type="discrete"
)
#rCharts likes data frame better
#no direct support for xts yet
roc.df <- data.frame(
format(index(roc),"%b %d, %Y"),
format(index(roc),"%Y"),
roc[,1],
roc[,2]
)
colnames(roc.df) <- c("date","year","vix","spx")
n1 <- nPlot(
spx ~ vix,
group = "year",
data = roc.df,
type = "scatterChart"
)
n1$xAxis( #use d3 format to make percent format
tickFormat = "#!function(d){return d3.format('.2%')(d)}!#"
)
n1$yAxis( #use d3 format to make percent format
tickFormat = "#!function(d){return d3.format('.2%')(d)}!#"
)
n1$chart(
showDistX = T,
showDistY = T,
tooltipContent= # can style like regular html
"#!function(e,f,g,h){return '<h3>'+h.point.date+'</h3'}!#"
)
n1