christophergandrud / d3Network

Tools for creating D3 JavaScript network graphs from R.
http://christophergandrud.github.io/d3Network/
171 stars 56 forks source link

Sankey diagrams #7

Closed christophergandrud closed 10 years ago

christophergandrud commented 11 years ago

Add capabilities to create Sankey diagrams (http://bost.ocks.org/mike/sankey/).

Make sure that they work with bipartite networks. See #6. Thanks @SChamberlain and @pedroj.

sckott commented 11 years ago

I will give it a shot and see if I can do it. See also #6

christophergandrud commented 11 years ago

I started up a new branch to develop the Sankey diagrams in (https://github.com/christophergandrud/d3Network/tree/sankey) along with the very very basic beginnings of a d3Sankey command. Will work on it a bit more tomorrow.

timelyportfolio commented 11 years ago

Here's some code if it helps http://timelyportfolio.github.io/rCharts_d3_sankey/example_build_network_sankey.html.

sckott commented 11 years ago

Cool, I gave it a try for a bit, but glad to see you have already made more progress than I :+1:

christophergandrud commented 11 years ago

Scott, there is now a working d3Sankey function in the sankey branch. You should be able to reproduce Mike's Sankey diagram with the following code:

devtools::install_github("d3Network", "christophergandrud", ref = "sankey")
# Load energy projection data
library(RCurl)
URL <- "https://raw.github.com/christophergandrud/d3Network/sankey/JSONdata/energy.json"
Energy <- getURL(URL, ssl.verifypeer = FALSE)
EngLinks <- JSONtoDF(jsonStr = Energy, array = "links")
EngNodes <- JSONtoDF(jsonStr = Energy, array = "nodes")

# Plot
d3Sankey(Links = EngLinks, Nodes = EngNodes, Source = "source",
                Target = "target", Value = "value", NodeID = "name"
                , fontsize = 12, nodeWidth = 30, file = "TestSankey.html")

It would be great if you could play around with this and see if you can get it to do what you wanted, or if we should make other changes.


@timelyportfolio Thanks a lot for the example, showing how to do this with rCharts. That looks like a good approach too.

sckott commented 11 years ago

This is awesome, thanks.

I will see if I can change things around to match needs.

christophergandrud commented 11 years ago

Great, just let me know any tweaks you think would make it better.

pedroj commented 11 years ago

This is terrific! I managed to replicate the code with my HR dataset, a bipartite network of plant-frugivore interactions.

Minor suggestions: 1) ability to directly parse an input adjacency matrix (i.e., a weighted adjacency matrix) as json data. 2) given that in bipartite representations of plant-animal mutalisms we typically have just two sets of species, ability to include the labels within the rectangles representing the nodes, so that the taxa labels don't remain 'floating'. 3) ability to code by group (I already made some tweaks in my HR data for that) with color codes.

I'll open asap a github repo for this code and example, including the data.

Thanks a lot for this nice implementation in d3Network!

Best, -- Pedro

El 09/09/2013, a las 07:31, Christopher Gandrud notifications@github.com escribió:

Great, just let me know any tweaks you think would make it better.

— Reply to this email directly or view it on GitHub.

christophergandrud commented 11 years ago

Thanks, yeah any examples that you can give me would be much appreciated (in political science these plots aren't very common).

pedroj commented 11 years ago

Tony Hirst has implemented a way to parse an adjacency matrix into a json data ready for d3: http://blog.ouseful.info/2013/02/18/reshaping-horse-importexport-data-to-fit-a-sankey-diagram/

however, it requires a step through networkx in python. I'm familiar with networkx. Yet It would be great if we can implement the whole thing in R. That would be easy (I think) using his code for vectorizing the matrix and then using R to parse the [ and " characters in the positions required by json (i.e., using paste). I'll try to do that asap. Just wanted to share this potentially interesting way to handle the input directly from the adjacency matrix (or from an edge list, by the way) and get the json data file.

Best, -- Pedro

pedroj commented 11 years ago

I've just created a repo with my tests so far: https://github.com/pedroj/d3Network_test http://pedroj.github.io/d3Network_test/

The json data files have nodes grouped by genera (both for the plant and animal species sets).

christophergandrud commented 11 years ago

@pedroj the adjacency matrix to JSON issue is probably even easier. Right now d3Sankey only needs two data frames (links and nodes). It then does the conversion to JSON internally. Maybe it would be good to add a capability to internally turn an adjacency matrix into the JSON file, so that the user doesn't need to convert the matrix into two data frames.

Can you load an example matrix up to the GitHub repo you created?

pedroj commented 11 years ago

I see. Even easier as it's just a matter of parsing the links to the

var links = [ { "source" : 0, "target" : 18, "value" : 3 }, 

and the nodes to:

var nodes = [ { "name" : "Species_1" }, ...

This can be done directly form a vectorized from of the adjacency matrix. I'll upload the adj matrices asap.

christophergandrud commented 11 years ago

Sounds good.