0

I would like to create an interactive version of charts.PerformanceSummary() using rCharts.

This is my attempt so far...but am struggling to put it all together....

# Load xts and PerformanceAnalytics package
require(xts)
require(PerformanceAnalytics)
# Generate rtns data
set.seed(123)
X.stock.rtns <- xts(rnorm(1000,0.00001,0.0003), Sys.Date()-(1000:1))  
Y.stock.rtns <- xts(rnorm(1000,0.00003,0.0004), Sys.Date()-(1000:1))
Z.stock.rtns <- xts(rnorm(1000,0.00005,0.0005), Sys.Date()-(1000:1))
rtn.obj <- merge(X.stock.rtns , Y.stock.rtns, Z.stock.rtns)
colnames(rtn.obj) <- c("x.stock.rtns","y.stock.rtns","z.stock.rtns")

# The below output is what we are aiming for
charts.PerformanceSummary(rtn.obj,lwd=1,main="Performance of stocks x,y and z")

# So this is what I have tried to do to replicate the data and try and generate graphs

# custom function to convert xts to data.frame
xts.2.df <- function(xts.obj){
   df <- ggplot2:::fortify(xts.obj)
   df[,1] <- as.character(df[,1])
   df
}

# calculating the data for the top and bottom graph
cum.rtn <- do.call(merge,lapply(seq(ncol(rtn.obj)),function(y){cumprod(rtn.obj[,y]+1)-1}))
dd.rtn <- do.call(merge,lapply(seq(ncol(rtn.obj)),function(y){Drawdowns(rtn.obj[,y])}))

# Loading rCharts package
require(devtools)
install_github('rCharts', 'ramnathv',ref='dev')
require(rCharts)
# creating the first cumulative return graph
m1 <- mPlot(x = "Index", y = c("x.stock.rtns","y.stock.rtns","z.stock.rtns"), type = "Line", data = xts.2.df(cum.rtn),
             pointSize = 0, lineWidth = 1)
# Top cumulative return graph
m1

# Creating the individual bar graphs that are to be shown when one line is hovered over
m.x <- mPlot(x = "Index", y = c("x.stock.rtns"), type="Bar",data = xts.2.df(rtn.obj))
m.y <- mPlot(x = "Index", y = c("y.stock.rtns"), type="Bar",data = xts.2.df(rtn.obj))
m.z <- mPlot(x = "Index", y = c("z.stock.rtns"), type="Bar",data = xts.2.df(rtn.obj))

# Creating the drawdown graph
m2 <- mPlot(x = "Index", y = c("x.stock.rtns","y.stock.rtns","z.stock.rtns"), type = "Line", data = xts.2.df(dd.rtn),
            pointSize = 0, lineWidth = 1)
m2

So there are few parts to the question:

  1. How do you put three morris.js charts together so that they are linked?
  2. Can you make bold the line that is being hovered over in the top graph (m1)?
  3. How do you get the middle one (i.e. one of m.x, m.y, m.z)to change according to what's been hovered over, i.e if hovering over stock z, then stock z's returns (m.z) show up un the middle?
  4. Can you get it to make bold in the bottom graph, the same asset that is being made bold in the top graph?
  5. Can you change the information that is being displayed to in the floating box to display some stats about the asset being hovered over?
  6. How do you add axes labels?
  7. How do you add an overall title?
  8. BONUS: How do you integrate crossfilter.js into it so that a subset of time can be chosen...and all graphs get re-drawn?

Even if you can't answer all parts any help/comments/answers would be appreciated...

h.l.m
  • 13,015
  • 22
  • 82
  • 169
  • 1
    "Help me complete this project" is *WAY* too broad. You don't have "a few parts to the question"; you have 8 questions, and I'm sure you could find answers to some of them by searching (e.g. how to add axes labels, an overall title). – Joshua Ulrich Sep 12 '13 at 13:36
  • It's not clear how this is related to D3, so I'm removing the tag. – Lars Kotthoff Sep 12 '13 at 13:47
  • I did look up how to add axes and a title...but given that it's a relatively new package...I couldn't find much... – h.l.m Sep 12 '13 at 13:55
  • I thought this code might run since it seemed to have dummy examples but it retruned a bunch of errors from missing objects and functions. – IRTFM Sep 12 '13 at 14:17
  • @Dwin I edited the code to add in the dev branch install of rCharts...hopefully it will now run...(i.e. require(devtools) install_github('rCharts', 'ramnathv',ref='dev')) – h.l.m Sep 12 '13 at 14:31
  • Still no Drawdowns function and an error "Error: ggplot2 doesn't know how to deal with data of class xtszoo" which looks odd since I think the class name is `xts` and not `xtszoo`. – IRTFM Sep 12 '13 at 14:36
  • Hmm, I'm using ggplot2_0.9.3.1 which should have the fortify function...and using also xts_0.9-5 and zoo_1.7-10 – h.l.m Sep 12 '13 at 14:46
  • Drawdowns function comes from `PerformanceAnalytics` – h.l.m Sep 12 '13 at 14:50
  • Can you post your question on the github issues page for rCharts? – Ramnath Sep 12 '13 at 20:47
  • [done](https://github.com/ramnathv/rCharts/issues/250) – h.l.m Sep 12 '13 at 21:59

0 Answers0