August 22, 2014 Andrew Patterson

Dendrograms in R2D3

Tweet about this on TwitterShare on LinkedInShare on FacebookGoogle+Share on StumbleUponEmail to someone

Hi, I’m Andrew and this is my first post for Coppelia! If you like the look of this feel free to visit my blog dinner with data (and see what happens when a data scientist hits the kitchen!)

I was excited by James’s last post on the new package R2D3, and I thought I would try to help further develop the package. This is a great new package, built by James Thomson (and in collaboration with myself and Simon Raper at Coppelia) that utilises D3 visualisations inside R. You can quickly create very striking visualisations with a just a few lines of code. This has recently been shared with a recent post, but since then a couple of updates have been made to increase the functionality.

In particular to the function D3Dendro, which creates dendrograms based on a hclust object in R. I had been working on a number of alternatives to the usual static dendrogram found in the package so far, so I thought I would add these in and describe them below.

I have created two new distinct functionalities:

  • Collapsible nodes
  • Radial output (rather than the more traditional ‘linear’ dendrogram)

You can clone the package from James’s github repository or run the following in R:


I will include the example in the original post, so you can easily compare the differences.

Original dendrogram:

hc <- hclust(dist(USArrests), "ave") JSON<-jsonHC(hc) D3Dendro(JSON, file_out="USArrests_Dendo.html")

Collapsible dendrogram:

hc <- hclust(dist(USArrests), "ave") JSON<-jsonHC(hc) D3Dendro(JSON,collapsible=TRUE,file_out="USArrests_Dendo_collapse.html")

Radial dendrogram:

hc <- hclust(dist(USArrests), "ave") JSON<-jsonHC(hc) D3Dendro(JSON,radial=TRUE,file_out="USArrests_Dendo_radial.html")

Collapsible Radial dendrogram (with help from some code by Captain Anonymous):

hc <- hclust(dist(USArrests), "ave") JSON<-jsonHC(hc) D3Dendro(JSON,collapsible=TRUE,radial=TRUE,file_out="USArrests_Dendo_collapse_radial.html")

Tagged: , , , , , , , ,

About the Author

Andrew Patterson I currently work as an Insight Analyst in an award winning data science team at Channel 4. I am R mad, but love to branch out into Python, javascript/jquery and love building d3 visualisations.

Machine Learning and Analytics based in London, UK