Few weeks ago I saw my friend create network graphs in python. I am not sure if he was embedding d3.js script using d3py or simply using a library in python; but anyway, I wanted to see if R had a similar package. A quick search in google and baam… There’s a package called d3Network for making sankey, network and dendogram graphs. This package was created by Christopher Gandrud. In this post, I simply attempt to explore and write my thoughts on it..
1. d3SimpleNetwork – Exploring three products of google – Blogger, Youtube and Songza.
The above network is dynamic. You can actually move things around and click on nodes to magnify texts. WordPress doesn’t allow to embed javascript for security reasons so I just took a snapshot of the output. I think I might move this blog to a different platform. Anywho, the thought process in the network above – Youtube was paypal’s product which was acquired by google. So a table would be…
Source | Target |
Paypal | Youtube |
Youtube | |
PyroLabs | Blogger |
Blogger …. and so on. You would fill this in a data frame below |
Here’s the code to do all this.
install.packages(‘d3Network’)
library(d3Network)
googleacq<-data.frame()
googleacq<-edit(data.frame()) ## You can input your source/target stuff here. Remeber Only two columns
d3SimpleNetwork (googleacq, width = 400, height = 600, textColour = “black”,
linkColour = “red”,nodeColour = “green”,nodeClickColour=’orange’,
opacity = 1.1, charge = -300, fontsize = 14)
After you run the code above, you need to copy and paste the script it returns in a text editor and save it as .html file. Or you could just open d3_example1 in a browser, by saving it as a webpage in MsWord first. Alternatively, you can copy paste the code in javascript section of http://jsfiddle.net and hit RUN.
Thoughts: It would be cool to have an arrow in the model so that it’s much more easier to understand the flow. If you want to make more complicated force network then you can use d3ForceDirected. Nevertheless, this is a great package!
2. d3Tree – A tree graph to show my favorite songs from different Metallica Albums.
As suggested by the author, it is easier to import data from JSON file to R rather than going through list steps. Also, you can zoom and highlight different nodes.
Here’s the code to generate the tree above.
Metallica <- list(name = “Metallica”,
children = list(list(name = “Kill em all”,
children = list(list(name = “The Four Horsemen “),
list(name= ‘ Seek and Destroy’),
list(name = ‘Jump in the Fire’))),
list(name = “Ride the Lightning”,
children = list(list(name = “Creeping Death”),
list(name = “For Whom The Bell Tolls”),
list(name = “Fade TO Black”),
list(name = “Creeping Death”),
list(name = “Ride The Lightning”))),
list(name = “Master of Puppets”,
children = list(list(name = “Master of Puppets”),
list(name = “Sanitarium”),
list(name = “Orion”))),
list(name = “.. And Justice For all”,
children = list(list(name = “One”),
list(name = “Harvester of Sorrow”),
list(name = “Blackened”))),
list(name = “Metallica”,
children = list(list(name = “Sad But True”),
list(name = “EnterSandman”),
list(name = “Nothing Else Matters”),
list(name= ‘Unforgiven’),
list(name= ‘Wherever I may Roam’))),
list(name = “Load”,
children = list(list(name = “King Nothing”),
list(name = “Until it Sleeps”),
list(name = “Mama Said”))),
list(name = “Reload”,
children = list(list(name = “The Unforgiven II”),
list(name = “Better Than You”),
list(name = “The Memory Remains”))),
list(name = “St. Anger”,
children = list(list(name = “”))),
list(name = “Death Magnetic”,
children = list(list(name = “”)))))
d3Tree(List = Metallica, fontsize = 22, diameter = 800,zoom=TRUE, opacity = .8,
textColour = “#D95F0E”, linkColour = “#0000FF”,
nodeColour = “#FF3399”, height = 2000, width = 3000,)
Click d3tree_metallica and open the file in web browser to play around. After the last line of code a series of HTML and Javascript codes are outputted. Save them in a text editor, rename as .html and open it in your browser. Alternatively, you can copy paste the code in javascript section of http://jsfiddle.net and hit RUN.
Thoughts: Like Tableau, it would be nice if labels didn’t overlap. Also, it would really be cool to be able to rotate the tree instead of just dragging it. I will further explore other stuffs like Sankey and clusterdendro. Great Package overall!