我想创建一个交互式版本的charts.PerformanceSummary()
using rCharts
。
这是我迄今为止的尝试......但我正在努力将它们放在一起......
# 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
所以这个问题有几个部分:
- 您如何将三个 morris.js 图表放在一起以使它们相互关联?
- 您能否将顶部图表中悬停的线加粗(
m1
)? - 你如何让中间的(即
m.x
,m.y
,中的一个m.z
)根据悬停的内容而改变,即如果悬停在股票 z 上,那么股票 z 的回报 (m.z
) 会出现在中间? - 您能否让它在底部图表中加粗,与在顶部图表中加粗的相同资产?
- 您可以更改浮动框中显示的信息以显示有关悬停资产的一些统计信息吗?
- 如何添加轴标签?
- 如何添加整体标题?
- 奖励:您如何
crossfilter.js
融入其中,以便可以选择时间子集......并且所有图表都被重新绘制?
即使您无法回答所有部分,我们也将不胜感激任何帮助/评论/答案...