R2D3 is a new package for R I’ve been working on. As the name suggests this package uses R to produce D3 visualisations. It builds on some work I previously blogged about here.
There are some similar packages out there on CRAN already. Notably rjson and d3Network. However I found with these packages that they covered parts of the process (creating a json or creating a D3) but not the whole process and not ensuring the json was in the right format for the D3. So that was the thinking with this package. I was the aiming to create an end to end process for converting R objects into D3 visualisations. When i mentioned it to [email protected] he was keen to contribute. So we’ve been collaborating on it over the last few weeks. Its by no means finished, but I think it contains enough that its worth sharing.
You can clone the package from my github repository or run the following in R
Here are a few examples to demonstrate.
The first example takes an hierarchical clustering object and creates a d3 dendrogram. This can be useful when you have a large number of items in the dendrogram. You might need to alter the file_out location so you know where to find the html document.
hc <- hclust(dist(USArrests), "ave")
The second example takes a series of groupings assigned to records and compares them. This example compares the output from a number of hierarchical clusterings using different distance measures . It could also be used with tracking people or products through a process.
hc.ave <- hclust(dist(USArrests), "ave")
hc.single <- hclust(dist(USArrests), "single")
hc.ward <- hclust(dist(USArrests), "ward.D")
The third example takes nested values and converts it to a tree map. In this the populations of UK counties/shires are sub divided by region and country. This json could also be used to make a dendrogram using D3Dendro.
JSON<-jsonNestedData(structure=counties[,1:3], values=counties[,4], top_label="UK")
The last example takes the relationships between a number of celebrities and maps them out on a force directed graph.