Tuesday, July 16, 2013

Exploring Networks with Sankey

Motivated by a tweet from Tony Hirst (http://blog.ouseful.info/), I started experimenting with an rCharts implementation of the d3 sankey plugin.  While I was putting together examples, I found lots of gaps in my knowledge of sankeys and network analysis.  I did not want to let good learning go to waste, so I put together this quick tutorial documenting what I learned.  Click here or on the screenshot below to see it.

For those wondering how this might be useful in finance, sankeys could be employed well in Portfolio Appraisal/Holdings type reports where nodes represent household, client accounts, asset classes, and security types.  Edge weight would be $ or % in each.

image

13 comments:

  1. Thanks for the interesting implementation, when trying to replicate it I got stuck on two lines of code.

    sankeyPlot$setLib('libraries/widgets/d3_sankey')
    sankeyPlot$setTemplate(script = "libraries/widgets/d3_sankey/layouts/chart.html")



    I see on github you use it in multiple scripts, care to enlighten what it does and what it refers to?


    Thanks!

    ReplyDelete
  2. Sure, I wrote about this a little in http://timelyportfolio.github.io/rCharts_d3_horizon/, but I'm not a skilled tutorial writer so it might still be confusing. These two lines (probably going to get streamlined soon in the next ver of rCharts) let rCharts know where to find the config.yml, js and css and layout folders. Since these files and folders are custom, rCharts does not know where to find these.

    ReplyDelete
  3. Hi

    I'm trying to replicate this example using RStudio.(0.97.551) and R (v3.0.11)

    The output I get when executing "sankeyPlot$print(chartId = 'sankey1')" is:


    ./rCharts_d3_sankey-gh-pages/libraries/widgets/d3_sankey/layouts/chart.html

    When i try running http://localhost:8888/rCharts_d3_sankey-gh-pages/layouts/chart.html I get an error message "Uncaught SyntaxError: Unexpected token {" which is referring to line 8 "var params = {{{ chartParams }}};



    I should appreciate any help you could offer to point out where I'm going wrong very much


    Many thanks


    All the best


    Julian

    ReplyDelete
  4. I'm not sure exactly what might be going wrong, but I have 2 suggestions that might help. First, install the newest dev branch of rCharts.

    require(devtools)
    install.github("rCharts","ramnathv",ref="dev")

    Then, you should be able to ease the custom template pain now by replacing:

    sankeyPlot$setLib('libraries/widgets/d3_sankey')
    sankeyPlot$setTemplate(script = "libraries/widgets/d3_sankey/layouts/chart.html")

    with

    sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')




    Let me know if this works. Thanks for trying.

    ReplyDelete
  5. Hi,
    i tried to do some sankeys with your code. but i have got 2 questions:
    1. in your example the last node on the bottom does not show up completly when it is small. is it posible to get a free space under the last node?
    2. is it possible to write the values of one link as a label on the link? (so that you don´t have to hover the curser over the link to see the value)

    thx for your help

    ReplyDelete
  6. I think I understand what you might want. Let me know what you think about this example http://rcharts.io/viewer/?6679752#.UkD9dMasiM4 . The code is here https://github.com/timelyportfolio/rCharts_d3_sankey/blob/gh-pages/example_hirst_f1_2.R and the script template is https://github.com/timelyportfolio/rCharts_d3_sankey/blob/gh-pages/layouts/chart_static_title.html. There are still some ways to make it better, but I wanted to make sure I was headed in the right direction.

    ReplyDelete
  7. I replicated your example. It works well on ubuntu, but I found that the arrow sign is cracked with Windows as you can see the image below. Plus, I am wondering if there is any method when using another language other than English.

    ReplyDelete
  8. Hi,
    I replicated your example. It works well on ubuntu, but I found that the arrow sign is cracked with Windows as you can see the image below. Plus, I am wondering if there is any method when using another language other than English.

    ReplyDelete
  9. Hi, I really appreciate this work and example! I have been using d3
    sankey diagrams for a while and want to include it in a shiny app now. I
    have been following your example, but run into the same problem with the
    location of the lib and template files.
    For different reasons (e.g. shiny app has to run on a computer iwthout internet connection - sorry for tis..), I need to use everything from the local volume.
    I see that the shiny app is rendering everything fine, except that the code is linking to the js and css on my local disk, which cannot be accessed via the localhost. Do you have any idea how to change this?

    ReplyDelete
  10. Well, after playing around the function renderChart2 did the trick. now it is working... :)

    ReplyDelete
  11. Hi Florian, I have ran in to the same issue while trying to implement rChart sankey chart in shiny however renderChart2 did not work for me. Can you past some code or go in a greater detail as to how did you get this working?

    ReplyDelete