August 21, 2014 James Thomson

Introducing R2D3

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

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.

The package was built using an object orientated approach. The functions in the package come in two types. The first type take R objects, vectors and data.frames and convert them into json files. There are a number of these depending on the data structure required for the d3. The second type take the json file output wrap some JavaScript around them and create an html file containing the D3 visualisation.

You can clone the package from my github repository or run the following in R

install.packages("devtools")
library("devtools")
install_github("jamesthomson/R2D3")
library("R2D3")

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")
plot(hc)
JSON<-jsonHC(hc)
D3Dendro(JSON, file_out="USArrests_Dendo.html")

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")
cut.ave<-cutree(hc.ave, k=6)
cut.single<-cutree(hc.single, k=6)
cut.ward<-cutree(hc.ward, k=6)
ClustComp<-data.frame(States=rownames(USArrests), ave=as.vector(cut.ave),single=as.vector(cut.single),ward=as.vector(cut.ward))
JSON<-jsonCompare(ClustComp)
D3Sankey(JSON, file_out="Sankey.html")

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.

data(counties)
JSON<-jsonNestedData(structure=counties[,1:3], values=counties[,4], top_label="UK")
D3Tree(JSON, file_out="Tree.html")

The last example takes the relationships between a number of celebrities and maps them out on a force directed graph.

data(celebs)
colnames(celebs$relationships)<-c('source', 'target')
colnames(celebs$celebs)<-c('name', 'group')
JSON<-jsonNodesLinks(celebs$celebs, celebs$relationships)
D3Force(JSON, file_out="Force.html")

Tagged: , , , ,

About the Author

James Thomson I have over 10 years experience working in analytics and statistics. I worked as a Statistician in the Pharma industry before branching out into analytics and data mining. I'm currently enjoying learning about data visualisation and machine learning. Over years I've worked with: GSK, Nectar, BP, Ford, Whitbread, Wunderman, Jaguar, RNIB, Virgin Media & Channel4. Please visit my own blog on analytics and music

Comment (1)

Comments are closed.

Machine Learning and Analytics based in London, UK