Path: blob/master/sandbox/RFinance2014/R/charting.R
1433 views
nvd3WeightsPlot <- function(object,1type=c("stackedAreaChart", "multiBarChart")2){3type <- match.arg(type)45# extract the weights and turn into a data.frame6weights <- extractWeights(object)7weights.df <- reshape2::melt(8data.frame(date=format(index(weights)), weights),9id.vars = 1,10variable.name = "stock",11value.name = "weight"12)13weights.df$date <- as.Date(weights.df$date)1415# plot16n1 <- rCharts::nPlot(17weight ~ date,18group = "stock",19data = weights.df,20type = type21)22n1$xAxis(23tickFormat = "#!function(d){24return d3.time.format('%b %Y')(new Date(d * 24 * 60 * 60 * 1000))25}!#"26)27n1$yAxis(28tickFormat = "#!function(d){29return d3.format('0.2%')(d)30}!#"31)32return(n1)33}3435nvd3RiskPlot <- function(object,36type=c("stackedAreaChart", "multiBarChart")37){38type <- match.arg(type)3940# extract the risk budget pct_contrib and turn into a data.frame41tmp <- extractObjectiveMeasures(object)42rb <- tmp[,grep("pct_contrib", colnames(tmp))]43colnames(rb) <- gsub("^.*\\.", "", colnames(rb))44rb.df <- reshape2::melt(45data.frame(date=as.Date(format(index(rb))), rb),46id.vars = 1,47variable.name = "fund",48value.name = "risk"49)5051# plot52n1 <- rCharts::nPlot(53risk ~ date,54group = "fund",55data = rb.df,56type = type57)58n1$xAxis(59tickFormat = "#!function(d){60return d3.time.format('%b %Y')(new Date(d * 24 * 60 * 60 * 1000))61}!#"62)63n1$yAxis(64tickFormat = "#!function(d){65return d3.format('0.2%')(d)66}!#"67)68return(n1)69}7071# require(rCharts)72# weights <- extractWeights(opt.minVarSample)73# weights.df <- reshape2::melt(74# data.frame(75# date=format(index(weights)),76# weights77# ),78# id.vars = 1,79# variable.name = "stock",80# value.name = "weight"81# )82#83# d1 <- dPlot(84# weight ~ date,85# groups = "stock",86# data = weights.df,87# type = "bubble" #area, bar, or bubble88# )89# d1$xAxis(90# type = "addTimeAxis",91# inputFormat = "%Y-%m-%d",92# outputFormat = "%b %Y"93# )94# d1$yAxis(95# outputFormat = "0.2%",96# orderBy = "weight"97# )98# d199100