-
-
Save srepho/8551697 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #do some quick exploration with R | |
| #to see how many years | |
| #the S&P 500 has gone all year | |
| #without going below the first day of the year | |
| require(quantmod) | |
| require(plyr) | |
| require(rCharts) | |
| getSymbols("^GSPC",from = "1950-01-01") | |
| #get closing price on day one of the year | |
| dayone <- GSPC[endpoints(GSPC[-NROW(GSPC),],"years")+1,] | |
| #make xts a data frame without first day of year | |
| GSPC.df <- data.frame( | |
| index(GSPC), | |
| as.numeric(format(index(GSPC),"%Y")), | |
| GSPC[,4] | |
| )[-(endpoints(GSPC[-NROW(GSPC),],"years")+1),] | |
| colnames(GSPC.df) <- c("date","year","price") | |
| minprice <- ddply(GSPC.df,.(year),summarize,yearmin=min(price)) | |
| minprice$dayone = dayone[-NROW(dayone),1] | |
| minprice$pctlower = minprice$yearmin / minprice$dayone - 1 | |
| minprice$higherorlower = minprice$yearmin < minprice$dayone | |
| #draw a quick chart with rCharts dimple | |
| d1 <- dPlot( | |
| pctlower ~ year, | |
| groups = "higherorlower", | |
| data = minprice, | |
| type = "bar" | |
| ) | |
| d1$xAxis( | |
| orderRule = "year" | |
| ) | |
| d1$yAxis( | |
| outputFormat = "#.1%" | |
| ) | |
| d1 | |
| d1$templates$script = | |
| "http://timelyportfolio.github.io/rCharts_dimple_systematic/libraries/widgets/dimple/layouts/chart.html" | |
| d1$defaultColors( | |
| RColorBrewer::brewer.pal(11,"RdYlGn")[c(9,2)], | |
| replace = T | |
| ) | |
| d1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype HTML> | |
| <meta charset = 'utf-8'> | |
| <html> | |
| <head> | |
| <script src='http://d3js.org/d3.v3.min.js' type='text/javascript'></script> | |
| <script src='http://dimplejs.org/dist/dimple.v1.1.3.min.js' type='text/javascript'></script> | |
| <style> | |
| .rChart { | |
| display: block; | |
| margin-left: auto; | |
| margin-right: auto; | |
| width: 700px; | |
| height: 400px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id='chart2298527a5f' class='rChart dimple'></div> | |
| <script type="text/javascript"> | |
| (function(){ | |
| var opts = { | |
| "dom": "chart2298527a5f", | |
| "width": 700, | |
| "height": 400, | |
| "x": "year", | |
| "y": "pctlower", | |
| "groups": "higherorlower", | |
| "type": "bar", | |
| "id": "chart2298527a5f" | |
| }, | |
| data = [ | |
| { | |
| "year": "1950", | |
| "yearmin": 16.67, | |
| "dayone": [ 16.66 ], | |
| "pctlower": [ 0.00060024 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1951", | |
| "yearmin": 20.69, | |
| "dayone": [ 20.77 ], | |
| "pctlower": [ -0.0038517 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1952", | |
| "yearmin": 23.09, | |
| "dayone": [ 23.8 ], | |
| "pctlower": [ -0.029832 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1953", | |
| "yearmin": 22.71, | |
| "dayone": [ 26.54 ], | |
| "pctlower": [ -0.14431 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1954", | |
| "yearmin": 24.8, | |
| "dayone": [ 24.95 ], | |
| "pctlower": [ -0.006012 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1955", | |
| "yearmin": 34.58, | |
| "dayone": [ 36.75 ], | |
| "pctlower": [ -0.059048 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1956", | |
| "yearmin": 43.11, | |
| "dayone": [ 45.16 ], | |
| "pctlower": [ -0.045394 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1957", | |
| "yearmin": 38.98, | |
| "dayone": [ 46.2 ], | |
| "pctlower": [ -0.15628 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1958", | |
| "yearmin": 40.37, | |
| "dayone": [ 40.33 ], | |
| "pctlower": [ 0.00099182 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1959", | |
| "yearmin": 53.58, | |
| "dayone": [ 55.44 ], | |
| "pctlower": [ -0.03355 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1960", | |
| "yearmin": 52.2, | |
| "dayone": [ 59.91 ], | |
| "pctlower": [ -0.12869 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1961", | |
| "yearmin": 58.36, | |
| "dayone": [ 57.57 ], | |
| "pctlower": [ 0.013722 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1962", | |
| "yearmin": 52.32, | |
| "dayone": [ 71.55 ], | |
| "pctlower": [ -0.26876 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1963", | |
| "yearmin": 63.72, | |
| "dayone": [ 63.1 ], | |
| "pctlower": [ 0.0098257 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1964", | |
| "yearmin": 75.5, | |
| "dayone": [ 75.02 ], | |
| "pctlower": [ 0.0063983 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1965", | |
| "yearmin": 81.6, | |
| "dayone": [ 84.75 ], | |
| "pctlower": [ -0.037168 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1966", | |
| "yearmin": 73.2, | |
| "dayone": [ 92.43 ], | |
| "pctlower": [ -0.20805 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1967", | |
| "yearmin": 80.55, | |
| "dayone": [ 80.33 ], | |
| "pctlower": [ 0.0027387 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1968", | |
| "yearmin": 87.72, | |
| "dayone": [ 96.47 ], | |
| "pctlower": [ -0.090702 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1969", | |
| "yearmin": 89.2, | |
| "dayone": [ 103.86 ], | |
| "pctlower": [ -0.14115 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1970", | |
| "yearmin": 69.29, | |
| "dayone": [ 92.06 ], | |
| "pctlower": [ -0.24734 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1971", | |
| "yearmin": 90.16, | |
| "dayone": [ 92.15 ], | |
| "pctlower": [ -0.021595 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1972", | |
| "yearmin": 102.09, | |
| "dayone": [ 102.09 ], | |
| "pctlower": [ 0 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1973", | |
| "yearmin": 92.16, | |
| "dayone": [ 118.06 ], | |
| "pctlower": [ -0.21938 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1974", | |
| "yearmin": 62.28, | |
| "dayone": [ 97.55 ], | |
| "pctlower": [ -0.36156 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1975", | |
| "yearmin": 70.04, | |
| "dayone": [ 68.65 ], | |
| "pctlower": [ 0.020248 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1976", | |
| "yearmin": 92.58, | |
| "dayone": [ 90.19 ], | |
| "pctlower": [ 0.0265 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1977", | |
| "yearmin": 90.71, | |
| "dayone": [ 107.46 ], | |
| "pctlower": [ -0.15587 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1978", | |
| "yearmin": 86.9, | |
| "dayone": [ 95.1 ], | |
| "pctlower": [ -0.086225 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1979", | |
| "yearmin": 96.13, | |
| "dayone": [ 96.11 ], | |
| "pctlower": [ 0.00020809 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1980", | |
| "yearmin": 98.22, | |
| "dayone": [ 107.94 ], | |
| "pctlower": [ -0.09005 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1981", | |
| "yearmin": 112.77, | |
| "dayone": [ 135.76 ], | |
| "pctlower": [ -0.16934 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1982", | |
| "yearmin": 102.42, | |
| "dayone": [ 122.55 ], | |
| "pctlower": [ -0.16426 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1983", | |
| "yearmin": 139.97, | |
| "dayone": [ 140.65 ], | |
| "pctlower": [ -0.0048347 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1984", | |
| "yearmin": 147.82, | |
| "dayone": [ 164.93 ], | |
| "pctlower": [ -0.10374 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1985", | |
| "yearmin": 163.68, | |
| "dayone": [ 167.2 ], | |
| "pctlower": [ -0.021053 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1986", | |
| "yearmin": 203.49, | |
| "dayone": [ 211.28 ], | |
| "pctlower": [ -0.036871 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1987", | |
| "yearmin": 223.92, | |
| "dayone": [ 242.17 ], | |
| "pctlower": [ -0.07536 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1988", | |
| "yearmin": 242.63, | |
| "dayone": [ 247.1 ], | |
| "pctlower": [ -0.01809 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1989", | |
| "yearmin": 279.43, | |
| "dayone": [ 277.72 ], | |
| "pctlower": [ 0.0061573 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1990", | |
| "yearmin": 295.46, | |
| "dayone": [ 353.4 ], | |
| "pctlower": [ -0.16395 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1991", | |
| "yearmin": 311.49, | |
| "dayone": [ 330.2 ], | |
| "pctlower": [ -0.056663 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1992", | |
| "yearmin": 394.5, | |
| "dayone": [ 417.03 ], | |
| "pctlower": [ -0.054025 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1993", | |
| "yearmin": 429.05, | |
| "dayone": [ 435.7 ], | |
| "pctlower": [ -0.015263 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1994", | |
| "yearmin": 438.92, | |
| "dayone": [ 466.51 ], | |
| "pctlower": [ -0.059141 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1995", | |
| "yearmin": 460.34, | |
| "dayone": [ 459.21 ], | |
| "pctlower": [ 0.0024607 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "1996", | |
| "yearmin": 598.48, | |
| "dayone": [ 615.93 ], | |
| "pctlower": [ -0.028331 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1997", | |
| "yearmin": 737.65, | |
| "dayone": [ 740.74 ], | |
| "pctlower": [ -0.0041715 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1998", | |
| "yearmin": 927.69, | |
| "dayone": [ 970.43 ], | |
| "pctlower": [ -0.044042 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "1999", | |
| "yearmin": 1212.2, | |
| "dayone": [ 1229.2 ], | |
| "pctlower": [ -0.013862 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2000", | |
| "yearmin": 1264.7, | |
| "dayone": [ 1469.2 ], | |
| "pctlower": [ -0.13919 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2001", | |
| "yearmin": 965.8, | |
| "dayone": [ 1320.3 ], | |
| "pctlower": [ -0.26849 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2002", | |
| "yearmin": 776.76, | |
| "dayone": [ 1148.1 ], | |
| "pctlower": [ -0.32343 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2003", | |
| "yearmin": 800.73, | |
| "dayone": [ 879.82 ], | |
| "pctlower": [ -0.089893 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2004", | |
| "yearmin": 1063.2, | |
| "dayone": [ 1111.9 ], | |
| "pctlower": [ -0.043789 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2005", | |
| "yearmin": 1137.5, | |
| "dayone": [ 1211.9 ], | |
| "pctlower": [ -0.061407 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2006", | |
| "yearmin": 1223.7, | |
| "dayone": [ 1248.3 ], | |
| "pctlower": [ -0.019707 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2007", | |
| "yearmin": 1374.1, | |
| "dayone": [ 1418 ], | |
| "pctlower": [ -0.030965 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2008", | |
| "yearmin": 752.44, | |
| "dayone": [ 1468 ], | |
| "pctlower": [ -0.48743 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2009", | |
| "yearmin": 676.53, | |
| "dayone": [ 902.99 ], | |
| "pctlower": [ -0.25079 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2010", | |
| "yearmin": 1022.6, | |
| "dayone": [ 1116.6 ], | |
| "pctlower": [ -0.084169 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2011", | |
| "yearmin": 1099.2, | |
| "dayone": [ 1257.6 ], | |
| "pctlower": [ -0.12594 ], | |
| "higherorlower": [ true ] | |
| }, | |
| { | |
| "year": "2012", | |
| "yearmin": 1277.3, | |
| "dayone": [ 1258.9 ], | |
| "pctlower": [ 0.014648 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "2013", | |
| "yearmin": 1457.2, | |
| "dayone": [ 1426.2 ], | |
| "pctlower": [ 0.021708 ], | |
| "higherorlower": [ false ] | |
| }, | |
| { | |
| "year": "2014", | |
| "yearmin": 1819.2, | |
| "dayone": [ 1845.9 ], | |
| "pctlower": [ -0.014443 ], | |
| "higherorlower": [ true ] | |
| } | |
| ], | |
| xAxis = { | |
| "type": "addCategoryAxis", | |
| "showPercent": false, | |
| "orderRule": "year" | |
| }, | |
| yAxis = { | |
| "type": "addMeasureAxis", | |
| "showPercent": false, | |
| "outputFormat": "#.1%" | |
| }, | |
| zAxis = [], | |
| colorAxis = [], | |
| defaultColors = [ | |
| [ "#66BD63", "#D73027" ] | |
| ], | |
| legend = []; | |
| var svg = dimple.newSvg("#" + opts.id, opts.width, opts.height); | |
| //data = dimple.filterData(data, "Owner", ["Aperture", "Black Mesa"]) | |
| var myChart = new dimple.chart(svg, data); | |
| if (opts.bounds) { | |
| myChart.setBounds(opts.bounds.x, opts.bounds.y, opts.bounds.width, opts.bounds.height);//myChart.setBounds(80, 30, 480, 330); | |
| } | |
| //dimple allows use of custom CSS with noFormats | |
| if(opts.noFormats) { myChart.noFormats = opts.noFormats; }; | |
| //for markimekko and addAxis also have third parameter measure | |
| //so need to evaluate if measure provided | |
| //x axis | |
| var x; | |
| if(xAxis.measure) { | |
| x = myChart[xAxis.type]("x",opts.x,xAxis.measure); | |
| } else { | |
| x = myChart[xAxis.type]("x", opts.x); | |
| }; | |
| if(!(xAxis.type === "addPctAxis")) x.showPercent = xAxis.showPercent; | |
| if (xAxis.orderRule) x.addOrderRule(xAxis.orderRule); | |
| if (xAxis.grouporderRule) x.addGroupOrderRule(xAxis.grouporderRule); | |
| if (xAxis.overrideMin) x.overrideMin = xAxis.overrideMin; | |
| if (xAxis.overrideMax) x.overrideMax = xAxis.overrideMax; | |
| if (xAxis.overrideMax) x.overrideMax = xAxis.overrideMax; | |
| if (xAxis.inputFormat) x.dateParseFormat = xAxis.inputFormat; | |
| if (xAxis.outputFormat) x.tickFormat = xAxis.outputFormat; | |
| //y axis | |
| var y; | |
| if(yAxis.measure) { | |
| y = myChart[yAxis.type]("y",opts.y,yAxis.measure); | |
| } else { | |
| y = myChart[yAxis.type]("y", opts.y); | |
| }; | |
| if(!(yAxis.type === "addPctAxis")) y.showPercent = yAxis.showPercent; | |
| if (yAxis.orderRule) y.addOrderRule(yAxis.orderRule); | |
| if (yAxis.grouporderRule) y.addGroupOrderRule(yAxis.grouporderRule); | |
| if (yAxis.overrideMin) y.overrideMin = yAxis.overrideMin; | |
| if (yAxis.overrideMax) y.overrideMax = yAxis.overrideMax; | |
| if (yAxis.inputFormat) y.dateParseFormat = yAxis.inputFormat; | |
| if (yAxis.outputFormat) y.tickFormat = yAxis.outputFormat; | |
| //z for bubbles | |
| var z; | |
| if (!(typeof(zAxis) === 'undefined') && zAxis.type){ | |
| if(zAxis.measure) { | |
| z = myChart[zAxis.type]("z",opts.z,zAxis.measure); | |
| } else { | |
| z = myChart[zAxis.type]("z", opts.z); | |
| }; | |
| if(!(zAxis.type === "addPctAxis")) z.showPercent = zAxis.showPercent; | |
| if (zAxis.orderRule) z.addOrderRule(zAxis.orderRule); | |
| if (zAxis.overrideMin) z.overrideMin = zAxis.overrideMin; | |
| if (zAxis.overrideMax) z.overrideMax = zAxis.overrideMax; | |
| } | |
| if(d3.keys(colorAxis).length > 0) { | |
| myChart[colorAxis.type](colorAxis.colorSeries,colorAxis.palette) ; | |
| } | |
| //allow manipulation of default colors to use with dimple | |
| if(defaultColors.length) { | |
| defaultColors = defaultColors[0]; | |
| if (typeof(defaultColors) == "function") { | |
| //assume this is a d3 scale | |
| //for now loop through first 20 but need a better way to handle | |
| defaultColorsArray = []; | |
| for (var n=0;n<20;n++) { | |
| defaultColorsArray.push(defaultColors(n)); | |
| }; | |
| defaultColors = defaultColorsArray; | |
| } | |
| defaultColors.forEach(function(d,i) { | |
| defaultColors[i] = new dimple.color(d); | |
| }) | |
| myChart.defaultColors = defaultColors; | |
| } | |
| //here need think I need to evaluate group and if missing do null | |
| //as the first argument | |
| //if provided need to use groups from opts | |
| if(opts.hasOwnProperty("groups")) { | |
| var s = myChart.addSeries( opts.groups, dimple.plot[opts.type] ); | |
| //series offers an aggregate method that we will also need to check if available | |
| //options available are avg, count, max, min, sum | |
| if (!(typeof(opts.aggregate) === 'undefined')) { | |
| s.aggregate = eval(opts.aggregate); | |
| } | |
| if (!(typeof(opts.lineWeight) === 'undefined')) { | |
| s.lineWeight = eval(opts.lineWeight); | |
| } | |
| if (!(typeof(opts.barGap) === 'undefined')) { | |
| s.barGap = eval(opts.barGap); | |
| } | |
| } else var s = myChart.addSeries( null, dimple.plot[opts.type] ); | |
| //unsure if this is best but if legend is provided (not empty) then evaluate | |
| if(d3.keys(legend).length > 0) { | |
| var l =myChart.addLegend(); | |
| d3.keys(legend).forEach(function(d){ | |
| l[d] = legend[d]; | |
| }); | |
| } | |
| //quick way to get this going but need to make this cleaner | |
| if(opts.storyboard) { | |
| myChart.setStoryboard(opts.storyboard); | |
| }; | |
| myChart.draw(); | |
| })(); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment