Last active
August 29, 2015 14:02
-
-
Save coppeliaMLA/002ec7f8a980ec18d23c to your computer and use it in GitHub Desktop.
Visualising cluster stability using a Sankey diagram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" | |
src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['sankey']}]}"> | |
</script> | |
<div id="sankey_basic" style="width: 900px; height: 300px;"></div> | |
<script type="text/javascript"> | |
google.setOnLoadCallback(drawChart); | |
function drawChart() { | |
var data = new google.visualization.DataTable(); | |
data.addColumn('string', 'From'); | |
data.addColumn('string', 'To'); | |
data.addColumn('number', 'Weight'); | |
data.addRows([ | |
["20.1","25.1", 5], | |
["20.2","25.2", 4], | |
["20.3","25.3", 3], | |
["20.4","25.4", 5], | |
["20.5","25.5", 3], | |
["25.1","30.1", 7], | |
["25.2","30.2", 4], | |
["25.3","30.3", 4], | |
["25.4","30.4", 5], | |
["25.4","30.6", 1], | |
["25.5","30.5", 4], | |
["30.1","35.1", 8], | |
["30.2","35.1", 3], | |
["30.2","35.4", 1], | |
["30.3","35.2", 4], | |
["30.4","35.3", 7], | |
["30.5","35.5", 5], | |
["30.6","35.3", 2], | |
["35.1","40.1",13], | |
["35.2","40.2", 4], | |
["35.3","40.2", 2], | |
["35.3","40.3", 8], | |
["35.4","40.4", 2], | |
["35.5","40.5", 6], | |
["40.1","45.1",14], | |
["40.2","45.2", 9], | |
["40.3","45.3", 9], | |
["40.4","45.4", 2], | |
["40.5","45.5", 6], | |
["45.1","50.1",14], | |
["45.2","50.2",11], | |
["45.3","50.3",10], | |
["45.4","50.4", 2], | |
["45.5","50.5", 8] | |
]); | |
// Set chart options | |
var options = { | |
width: 600, | |
}; | |
// Instantiate and draw our chart, passing in some options. | |
var chart = new google.visualization.Sankey(document.getElementById('sankey_basic')); | |
chart.draw(data, options); | |
} | |
</script> | |
</head> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Sequence for adding new data | |
s<-seq(20,50, by=5) | |
#Set up object for recording clusters | |
clus.change<-NULL | |
#Cycle through the clustering solutions | |
for (i in s){ | |
hc <- hclust(dist(USArrests[1:i,]), "ave") | |
ct<-cutree(hc, h=50) | |
add<-data.frame(size=rep(i, i), ind=names(ct), cluster=paste0(i, ".", ct)) | |
clus.change<-rbind(clus.change, add) | |
} | |
#Reshape the data | |
m<-merge(clus.change, clus.change, by="ind") | |
library(reshape2) | |
d<-dcast(data=m, size.x+size.y+cluster.x+cluster.y~., length) | |
#Filter to just the sequential steps | |
ex<-d[d$size.y==d$size.x+5,3:5] | |
#turn into a JS matrix | |
mat<-function(x){ | |
return(paste0('["',x[1], '","', x[2], '",', x[3], '],' )) | |
} | |
a<-apply(ex,1,mat) | |
#Output to a file | |
fileConn<-file("/path/sankeyMatrix.txt") | |
writeLines(a, fileConn) | |
close(fileConn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment