Showing posts with label d3. Show all posts
Showing posts with label d3. Show all posts

Wednesday, March 11, 2015

Extracting Heatmap

Inspired by this tweet, I wanted to try to do something similar in JavaScript.

Fortunately, I had this old post Chart from R + Color from Javascript to serve as a reference, and I got lots of help from these links.

In a couple of hours, I got this crude but working rendering complete with a d3.js brush to get the scale.  Then since this is sort of a finance blog, I imagined we found an old correlation heatmap like the one in Pretty Correlation Map of PIMCO Funds.  Although, we could guess at the correlation values, I thought it would be a lot more fun to get live values.  Try it out below.

  1. Brush over the scale / legend
  2. Input scale min and max
  3. Mouseover color areas in the chart

As I said, it is rough, but it works. It needs a little UI work :)

Thursday, October 9, 2014

SVG + a little extra (d3.js) in RStudio Browser | No Pipes This Time

I’m guessing here, but yesterday’s post Responsive SVG in Your RStudio Browser might have inspired some “but,…)”s, “yes plus I need”s, “what the %>>% with the pipe”s, etc.  I’ll attempt to address a couple of these in this quick post.

First, if you don’t like pipes, here is the non-piped version of the code.  I also made one change, which assumes that you want the SVG to fill the <div> container.  This is helpful if you think you will only have one plot and nothing else in your HTML.

library(SVGAnnotation)
library(htmltools)

respXML <- function( svg_xml, height = NULL, width = "100%", print = T, ... ){
# svg_xml should be an XML document
library(htmltools)
library(XML)

svg <- structure(
ifelse(
length(getDefaultNamespace(svg_xml)) > 0
,getNodeSet(svg_xml,"//x:svg", "x")
,getNodeSet(svg_xml,"//svg")
)
,class="XMLNodeSet"
)

xmlApply(
svg
,function(s){
a = xmlAttrs(s)
removeAttributes(s)
xmlAttrs(s) <- a[-(1:2)]
xmlAttrs(s) <- c(
style = paste0(
"height:100%;width:100%;"
)
)
}
)

svg <- HTML( saveXML( svg_xml) )

svg <- tags$div(
style = paste(
sprintf('width:%s;',width)
,ifelse(!is.null(height),sprintf('height:%s;',height),"")
)
,svg
)

if(print) html_print(svg)

return( invisible( svg ) )
}



Second, I like it but I’m helpless without my Javascript helper libraries, such as d3.js, Snap.svg, Raphaël, etc.  htmltools makes it fairly easy to attach dependencies.  Let’s add d3.js in this example.


Third, I want to use my helper library to add some script to make something awesome.  I can’t help with the awesome part, but I can show you how to add a little bit of code.  This time we’ll take the simple pan/zoom code from ggplot2 meet d3, and here is the result.  Please understand that this is only 4 lines of Javascript, so the pan/zoom is not nearly as refined as I would expect.


R_svg_d3js



# make our plot here
# since we will need to manipulate to add a g container
# for smoother d3 pan/zoom
sP = respXML(
svgPlot(
dotchart(
t(VADeaths)
, xlim = c(0,100)
, main = "Death Rates in Virginia - 1940"
)
)
, height = "100%"
, print = F
)

# parse the plot html with rvest
sP = html(as.character(sP))
# add a g node to contain the plot
# for smoother d3 pan / zoom
g = newXMLNode("g")
# add the old g to our new g container
addChildren(g, html_nodes(sP,"svg > g"))
# add our new g container to our svg
addChildren(html_nodes(sP,"svg")[[1]],g)

html_print(attachDependencies(
tagList(
# get the div with our modified svg
HTML(saveXML(html_nodes(sP,"div")[[1]]))
, tags$script(
HTML(
'
var g = d3.select("svg > g");
var zoom = d3.behavior.zoom().scaleExtent([1, 8]).on("zoom", zoomed)
g.call(zoom)

function zoomed() {
g.select("g")
.attr(
"transform",
"translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")"
);
}
'

)
)
)
,htmlDependency(
name="d3"
,version="3.0"
,src=c("href"="http://d3js.org/")
,script="d3.v3.js"
)
))

Wednesday, July 16, 2014

Pick a Color Site built in R with Shiny tags %$%

I started down the color path with yesterday’s post Palette of Colors from Image %>% ggplot2 %>% rCharts + dimple.js.  Although R has lots of tools, such as RColorBrewer and the mentioned rPlotter, javascript does too with Gregor Aisch's chroma.js and this translated-from-Perl color-scheme-js.

I figured I should explore these javscript color tools but not in the easy way.  Rather, I thought I should make it a little more challenging by forcing myself to build the site all in R using Shiny Tags based on an idea proposed by Ramnath Vaidyanathan in this rChartsDygraphs issue.  While I was at it, I thought I should keep plugging away with magrittr by playing with the new %$%.

I’m sure the result would amuse professional web developers and color pickers and probably even diehard R coders, but at least there was a result.

image

Thursday, July 3, 2014

Beer and Pie | rCharts pie charts with d3pie

In honor of the 4th of July, I thought a quick example of a pie chart on beer using the wonderful new d3pie library would be appropriate.  The rCharts binding with d3pie is simply an experiment now, but expect more in the near future.

 

Using slidify and rCharts, I created a more complete writeup of the experiment with d3pie if you would like to play with it yourself.

Monday, June 23, 2014

A Whole New R World with Chains and Pipes

I think that the recent shift in the R world to chains and pipes will become permanent.  Even if not, this style of code translates well to Javascript and other languages.  I thought a finance example exploring Fama/French factors with dplyr, magrittr, tidyr, and rCharts would help me learn and think through new workflows.

image

Wednesday, June 4, 2014

Active Share and Tracking Error | Not Mutually Exclusive Decisions

Antti Petajisto got a lot of attention with his research on Active Share and Tracking Error in mutual fund management.  While the research is fairly compelling, there is a missing discussion about a potential relationship between the two measures. The paper seems to suggest that mutual fund managers can independently and intentionally pursue Active Share and/or Tracking Error.  However, some research from the PIMCO Quantitative Team argues that Active Share and Tracking Error are related and that they can easily be a byproduct of other decisions such as benchmark or fund exposure.  I strongly suggest reading both sets of research to gather a full understanding of these concepts.

Table 2 is simple enough, but despite its simplicity I could not visualize in my head, so I copied the data and with a couple lines of R + rCharts got all the visualization I needed.

image

 

replicate:

source code from Github

Thursday, May 15, 2014

PDF Chart–> Inkscape SVG –> d3 Interactive Chart

I just spent the last couple of days updating myself on the newest research on currency management.  For the best real-time (delayed about a year) broad look, BIS puts out an incredibly helpful Triennial Central Bank Survey of foreign exchange... in which you see things like this

image

and this.

image

pdfs suffer from a lack of interactivity, so I thought why not use Inkscape’s PDF to SVG conversion to strip the above chart and make it SVG.  Then, if everything goes well, experiment with some basic interactivity provided by d3.js.  Here is what happened.

If anyone would like a more extensive write up of the process, please let me know.

Tuesday, May 6, 2014

High Frequency | Winners and Losers by Chord

I admit it looks like I am the only person in the world who has not read Michael Lewis’  Flash Boys.

I was more attracted to this fine paper with slightly fewer views than Flash Boys.

Baron, Matthew and Brogaard, Jonathan and Kirilenko, Andrei A.,
Risk and Return in High Frequency Trading
May 5, 2014
Available at SSRN: http://ssrn.com/abstract=2433118

The paper offers an incredible view of the players and their effects in the E-mini S&P 500 futures contract.   The data was so good that I just had to unlock the data from their tables.  By far my favorite table was table 7 with “the decomposition of average daily short-term profits among different trader types in August 2010.”  The profits are given as average daily $ profit and profit per trade per contract.  The d3 chord layout seemed perfect to visualize this data.  Fortunately, Ben Hunter had just blogged about his custom rCharts implementation of the chord layout in Chord Diagram with rCharts.  In beautiful open source fashion, I was able to adapt it and quickly view the data as below.  I am not sure how exactly it ties to Flash Boys, but I am guessing that this lends some credibilty to the story.  Just look at the miserable losses of “Small Traders”.

As a comparison between interactive and static graphs, I’ll add some ggplot2 versions.  I will admit that I spent little time customizing any of the charts above or below.  I will save that for later.

image

image

Source at Github: https://github.com/timelyportfolio/rCharts_chord

Wednesday, April 23, 2014

All the Factors | More Looks

Well, my last post Exploring Factors with rCharts and factorAnalytics got enough attention to motivate me to pull in some more Asness, Frazzini, Pedersen factors and plot them in some different ways.  The additional factors are US and global UMD (up minus down) and QMJ (quality minus junk) introduced in this paper.

Quality Minus Junk

Asness, Clifford S. and Frazzini, Andrea and Pedersen, Lasse Heje

October 9, 2013

Available at SSRN: http://ssrn.com/abstract=2312432

I used rCharts and dimplejs to draw a cumulative line chart.

Then I thought this would be a great opportunity to use the correlation chart spawned by rCharts issue 381.

 

Of course the next step is to simply merge the BAB (Betting Against  Beta) factors from the previous post, but I’ll leave that to you.  Let me know what you discover.

Monday, April 21, 2014

Exploring Factors with rCharts and factorAnalytics

Fama and French changed the financial world with their factors in 1993.  Another duo Andrea Frazzini and Lasse Heje Pedersen have expanded our world with their Betting Against Beta (BAB) and Quality Minus Junk (QMJ)  factors.  The combined factor set of Fama/French and Frazzini/Pedersen provides substantial insight into the historical performance of equities in the US and around the world.

Fortunately for us, the authors have also made available their factors.  Unfortunately, BAB and QMJ are not updated like the Fama/French SMB and HML. (actually discovered this is no longer true Frazzini data library)  Still though, combining these factors with the R packages factorAnalytics and rCharts allows us to do some amazing things.  Here is a quick example. Look for more soon.

Friday, April 4, 2014

R as a Publishing Engine | CPI Components Use Case

R was certainly not designed to be a publishing engine, but in my workflow, R is the primary method of content creation.  With that in mind, I have been thinking about a very different use case of rCharts in which we might want to include inflexible and not really reusable custom javascript components in our document.  As a quick example after updating my CPI component graph using d3.js and angular, I want to plug it into a document and really only need to modify a couple of parameters.  While someone might want to use this for different data, I doubt it.

rCharts seamless integration with knitr/slidify/Rpres makes this very easy.  I just wonder how popular this will become.

image

Wednesday, April 2, 2014

xts like endpoints in Javascript

I decided to promote this from a Twitter comment to a blog post.  I had hoped to do a prototype javascript interactive rebalancing visualization of Unsolved Mysteries of Rebalancing integrating this, but I have not had the time, so  I’ll release it into the wild in its current state.  I hope someone can use it.

image

Friday, March 28, 2014

Slopegraphs | rCharts –> maybe finance versions

Back in 2011, Charlie Park did two very thorough posts on Edward Tufte’s table graphics or slopegraphs.

http://charliepark.org/slopegraphs/

http://charliepark.org/a-slopegraph-update/

These type graphics can provide very effective visualizations of finance and economic information.  For my first test though I will stick with cancer survivor data from this post Slopegraphs in R | James Keirstead.  We can use a dimplejs line chart from rCharts as our platform and add some javascript to do something similar.

If you know of any good finance or economics slopegraphs, please let me know and I might just try to recreate them.

Click here or on the image below to see the fully post

image

Tuesday, March 25, 2014

Interactive Discovery of Research Affiliates JoPM Paper

In my previous post More on Rebalancing | With Data from Research Affiliates , I did some really basic visualizations, but I thought this data would be great for some more powerful interactive discovery using an interesting javascript SQL-like query language objeq along with the d3.js charting library dimple.js.  Next, I hope to extend to use lodash or lazy.js.

This exercise helps me think through a couple of lingering issues:

  1. After we create the plot, do we need to maintain the overhead of a connection with R using something like shiny, or can we port some of the aggregation, filtering, and calculations to javascript as we did in this example?
  2. How can we use the rCharts templates with other languages such as Python, Ruby, and Javascript?
  3. What can we do with some more specific and customized page templates for rCharts?
  4. Is a Lyra-like interface better or will this type interface work for more advanced users?

Help me with your thoughts after you have played with the example shown by the screenshot below.image

Thursday, March 20, 2014

Is It Structurally Broke? bfast breakpoints

The R package bfast enthralls me.  I have posted 3 times on bfast but still did not understand the impact of the h parameter.  Armed now with some d3.js, angular.js, and rCharts I thought I could see it better with a fancy interactive visualization.  Here is the result when applied to the S&P 500 monthly price series since 1950.  You should see it embedded in an iframe below.  For the full effect, click here.

Friday, March 14, 2014

Bond Shop Views with dimplejs and rCharts

I saw this chart in a presentation and thought I could make it better and interactive.  Here is a short article on the iteration process.  Click on the screenshot below or here for the full post.

Note: these are not my views and this is not financial advice.  I did not name the original source of the graphic.  If you are the source, and you happen to see this, let me know if you would like me to name you.

image

Monday, February 24, 2014

More on Rebalancing | With Data from Research Affiliates

While on the topic of rebalancing (see Unsolved Mysteries of Rebalancing), I thought it would be good to highlight another good research paper with some quick rCharts analysis.

Arnott, Robert D., et al.
The Surprising Alpha from Malkiel’s Monkey and Upside-Down Strategies
The Journal of Portfolio Management 39.4 (2013): 91-105.

I have embedded in an iframe below, but you might get a better experience by clicking through to this full page.


Friday, February 21, 2014

Unsolved Mysteries of Rebalancing

There is a lot not yet fully understood about rebalancing in portfolio management.  This 2013 paper from Nardon and Kiskiras is the best I have read yet.

Kiskiras, John and Nardon, Andrea
Portfolio Rebalancing: A Stable Source of Alpha
January 18, 2013
Available at SSRN: http://ssrn.com/abstract=2202736

I wanted to share both a summary and extension of the article that I assembled in R with rCharts and slidify.  I embedded it below, but it will look much better if you follow this link http://timelyportfolio.github.io/rCharts_nardon_kiskiras/Industry_French_RebalPrem.html.

Monday, January 27, 2014

Emerging Currencies with rCharts + FRED

I liked this chart a lot.

I thought I would show how we can semi-replicate it in R with rCharts.  Here it is with the currencies that are on FRED with dimplejs.

… and here with nvd3.

Code to replicate from Github Gist:

Tuesday, January 21, 2014

What Do the Odds Say? Buy Stocks Begin of Year?

At some point in 2013, I read (can’t remember where) that 2012 was a rare year for the S&P 500  where no day’s closing  price was lower than the closing price  for the first day.  So if you bought on the first day of 2012, you never had a loss for the entire year.  Well, the same thing happened 2013, so I just had to do some analysis.  Below is a dimple.js chart built using rCharts with the minimum close price for the year divided by the close price for the first day of that year.

data source: Yahoo! Finance, Standard & Poors