Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created July 15, 2013 21:15
Show Gist options
  • Save timelyportfolio/6003575 to your computer and use it in GitHub Desktop.
Save timelyportfolio/6003575 to your computer and use it in GitHub Desktop.
rCharts Sankey EU Horsemeat
#http://schoolofdata.org/2013/02/20/made-to-measure-reshaping-horsemeat-importexport-data-to-fit-a-sankey-diagram/
require(reshape)
horseexportsEU <- read.delim(
"https://dl.dropbox.com/u/1156404/horseexportsEU.txt"
)
#Get a "long" edge list from the 2d data table
x=melt(horseexportsEU,id='COUNTRY')
# When is what looks like a number to us not a number?
#Turn the numbers into numbers by removing the comma, then casting to an integer
x$value2=as.integer(as.character(gsub(",", "", x$value, fixed = TRUE) ))
#More tidying...
#1) If we have an NA (null/empty) value, make it -1
x$value2[ is.na(x$value2) ] = -1
#2) Column names with countries that originally contained spaces uses dots in place of spaces. Undo that.
x$variable=gsub(".", " ", x$variable, fixed = TRUE)
#I want to export a subset of the data
xt=subset(x,value2>0,select=c('COUNTRY','variable','value2'))
#name columns as what is expected by plugin
colnames(xt) <- c("target","source","value")
#need to make names in source and target different to prevent infinite loop
xt$source <- paste0(xt$source,"[export]")
sankeyPlot <- rCharts$new()
sankeyPlot$setLib('.')
sankeyPlot$setTemplate(script = "layouts/chart.html")
sankeyPlot$set(
data = xt,
nodeWidth = 15,
nodePadding = 10,
layout = 32,
width = 700,
height = 400
)
sankeyPlot
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<link rel='stylesheet' href="http://netdna.bootstrapcdn.com/bootswatch/2.3.1/cosmo/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-responsive.min.css" >
<link rel='stylesheet' href="http://twitter.github.io/bootstrap/assets/js/google-code-prettify/prettify.css">
<link rel='stylesheet' href="http://aozora.github.io/bootplus/assets/css/docs.css">
<link rel='stylesheet' href='http://timelyportfolio.github.io/rCharts_d3_sankey/css/sankey.css'>
<script src='http://d3js.org/d3.v3.min.js' type='text/javascript'></script>
<script src='http://timelyportfolio.github.io/rCharts_d3_sankey/js/sankey.js' type='text/javascript'></script>
<style>
.rChart {
display: block
margin: auto auto;
width: 100%;
height: 400px;
}
/*
body {
margin-top: 60px;
}
*/
</style>
</head>
<body>
<div class='container'>
<div class='row'>
<div class='span8'>
<div class="bs-docs-example">
<div id='chart25446ad474c6' class='rChart nvd3Plot rCharts_d3_sankey'></div>
<br/>
<pre><code class='r'>#http://schoolofdata.org/2013/02/20/made-to-measure-reshaping-horsemeat-importexport-data-to-fit-a-sankey-diagram/
require(reshape)
horseexportsEU &lt;- read.delim(
&quot;https://dl.dropbox.com/u/1156404/horseexportsEU.txt&quot;
)
#Get a &quot;long&quot; edge list from the 2d data table
x=melt(horseexportsEU,id='COUNTRY')
# When is what looks like a number to us not a number?
#Turn the numbers into numbers by removing the comma, then casting to an integer
x$value2=as.integer(as.character(gsub(&quot;,&quot;, &quot;&quot;, x$value, fixed = TRUE) ))
#More tidying...
#1) If we have an NA (null/empty) value, make it -1
x$value2[ is.na(x$value2) ] = -1
#2) Column names with countries that originally contained spaces uses dots in place of spaces. Undo that.
x$variable=gsub(&quot;.&quot;, &quot; &quot;, x$variable, fixed = TRUE)
#I want to export a subset of the data
xt=subset(x,value2&gt;0,select=c('COUNTRY','variable','value2'))
#name columns as what is expected by plugin
colnames(xt) &lt;- c(&quot;target&quot;,&quot;source&quot;,&quot;value&quot;)
#need to make names in source and target different to prevent infinite loop
xt$source &lt;- paste0(xt$source,&quot;[export]&quot;)
sankeyPlot &lt;- rCharts$new()
sankeyPlot$setLib('.')
sankeyPlot$setTemplate(script = &quot;layouts/chart.html&quot;)
sankeyPlot$set(
data = xt,
nodeWidth = 15,
nodePadding = 10,
layout = 32,
width = 700,
height = 400
)
sankeyPlot
</code></pre>
</div>
</div>
</div>
</div>
<!--Attribution:
Mike Bostock https://github.com/d3/d3-plugins/tree/master/sankey
Mike Bostock http://bost.ocks.org/mike/sankey/
-->
<script>
var params = {
"dom": "chart25446ad474c6",
"width": 700,
"height": 400,
"data": {
"target": [ "SLOVENIA", "AUSTRIA", "BULGARIA", "CYPRUS", "CZECH REPUBLIC", "DENMARK", "ESTONIA", "FINLAND", "FRANCE", "GERMANY", "HUNGARY", "IRELAND", "ITALY", "LITHUANIA", "LUXEMBOURG", "MALTA", "NETHERLANDS", "POLAND", "PORTUGAL", "ROMANIA", "SLOVENIA", "SWEDEN", "UNITED KINGDOM", "BELGIUM", "GREECE", "ROMANIA", "POLAND", "SLOVAKIA", "BELGIUM", "FINLAND", "ITALY", "FINLAND", "LITHUANIA", "SWEDEN", "LATVIA", "SWEDEN", "BELGIUM", "BULGARIA", "CYPRUS", "CZECH REPUBLIC", "GERMANY", "ITALY", "LUXEMBOURG", "NETHERLANDS", "PORTUGAL", "SPAIN", "UNITED KINGDOM", "AUSTRIA", "BELGIUM", "FRANCE", "ITALY", "LUXEMBOURG", "NETHERLANDS", "SWEDEN", "BELGIUM", "CYPRUS", "AUSTRIA", "BULGARIA", "ESTONIA", "LATVIA", "POLAND", "SLOVENIA", "UNITED KINGDOM", "BELGIUM", "CZECH REPUBLIC", "DENMARK", "FRANCE", "GERMANY", "ITALY", "NETHERLANDS", "SWEDEN", "UNITED KINGDOM", "AUSTRIA", "BELGIUM", "BULGARIA", "CZECH REPUBLIC", "DENMARK", "FINLAND", "FRANCE", "GERMANY", "GREECE", "HUNGARY", "MALTA", "NETHERLANDS", "POLAND", "ROMANIA", "SLOVENIA", "ESTONIA", "ITALY", "LATVIA", "SWEDEN", "BELGIUM", "FRANCE", "GERMANY", "AUSTRIA", "BELGIUM", "CZECH REPUBLIC", "DENMARK", "FINLAND", "FRANCE", "GERMANY", "ITALY", "LUXEMBOURG", "MALTA", "SWEDEN", "CZECH REPUBLIC", "GERMANY", "ITALY", "MALTA", "SLOVAKIA", "SWEDEN", "ITALY", "AUSTRIA", "BELGIUM", "BULGARIA", "GERMANY", "GREECE", "HUNGARY", "ITALY", "NETHERLANDS", "POLAND", "ITALY", "BELGIUM", "BULGARIA", "CYPRUS", "CZECH REPUBLIC", "DENMARK", "ESTONIA", "FRANCE", "GERMANY", "GREECE", "HUNGARY", "ITALY", "ROMANIA", "SLOVENIA", "ESTONIA", "POLAND", "BELGIUM", "FRANCE", "GERMANY", "ITALY", "POLAND" ],
"source": [ "AUSTRIA[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BELGIUM[export]", "BULGARIA[export]", "BULGARIA[export]", "BULGARIA[export]", "CZECH REPUBLIC[export]", "CZECH REPUBLIC[export]", "DENMARK[export]", "DENMARK[export]", "DENMARK[export]", "ESTONIA[export]", "ESTONIA[export]", "ESTONIA[export]", "FINLAND[export]", "FINLAND[export]", "FRANCE[export]", "FRANCE[export]", "FRANCE[export]", "FRANCE[export]", "FRANCE[export]", "FRANCE[export]", "FRANCE[export]", "FRANCE[export]", "FRANCE[export]", "FRANCE[export]", "FRANCE[export]", "GERMANY[export]", "GERMANY[export]", "GERMANY[export]", "GERMANY[export]", "GERMANY[export]", "GERMANY[export]", "GERMANY[export]", "GREECE[export]", "GREECE[export]", "HUNGARY[export]", "HUNGARY[export]", "HUNGARY[export]", "HUNGARY[export]", "HUNGARY[export]", "HUNGARY[export]", "HUNGARY[export]", "IRELAND[export]", "IRELAND[export]", "IRELAND[export]", "IRELAND[export]", "IRELAND[export]", "IRELAND[export]", "IRELAND[export]", "IRELAND[export]", "IRELAND[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "ITALY[export]", "LITHUANIA[export]", "LITHUANIA[export]", "LITHUANIA[export]", "LITHUANIA[export]", "LUXEMBOURG[export]", "LUXEMBOURG[export]", "LUXEMBOURG[export]", "NETHERLANDS[export]", "NETHERLANDS[export]", "NETHERLANDS[export]", "NETHERLANDS[export]", "NETHERLANDS[export]", "NETHERLANDS[export]", "NETHERLANDS[export]", "NETHERLANDS[export]", "NETHERLANDS[export]", "NETHERLANDS[export]", "NETHERLANDS[export]", "POLAND[export]", "POLAND[export]", "POLAND[export]", "POLAND[export]", "POLAND[export]", "POLAND[export]", "PORTUGAL[export]", "ROMANIA[export]", "ROMANIA[export]", "ROMANIA[export]", "ROMANIA[export]", "ROMANIA[export]", "ROMANIA[export]", "ROMANIA[export]", "ROMANIA[export]", "ROMANIA[export]", "SLOVENIA[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SPAIN[export]", "SWEDEN[export]", "SWEDEN[export]", "UNITED KINGDOM[export]", "UNITED KINGDOM[export]", "UNITED KINGDOM[export]", "UNITED KINGDOM[export]", "UNITED KINGDOM[export]" ],
"value": [ 1200, 1.346e+05, 1.819e+05, 2.006e+05, 38200, 76100, 34300, 8.328e+05, 5.8989e+06, 1.108e+06, 43000, 11500, 2.5731e+06, 1700, 1.137e+05, 55700, 7.2652e+06, 3700, 100, 21000, 8700, 2.19e+05, 4100, 35400, 34300, 2.263e+05, 300, 6300, 79500, 25400, 1.371e+05, 28600, 900, 1600, 5100, 3700, 2.3871e+06, 42000, 84800, 32500, 23700, 2.1543e+06, 11400, 5100, 100, 1000, 94900, 3700, 1.912e+05, 51000, 16000, 100, 42500, 24800, 600, 100, 13600, 8.272e+05, 1.109e+05, 1.083e+05, 3000, 500, 8000, 6.549e+05, 63400, 44600, 5.286e+05, 95800, 1.1725e+06, 89100, 73700, 1.672e+05, 93800, 2.257e+05, 71300, 84000, 40700, 32700, 20700, 34000, 100, 1.2389e+06, 1200, 2.869e+05, 71000, 100, 4800, 3500, 5.854e+05, 2700, 300, 400, 1.4111e+06, 2700, 1.098e+05, 1.4084e+06, 12300, 40900, 6.151e+05, 2.1025e+06, 38200, 9.734e+05, 5700, 700, 3800, 1100, 75300, 1.0571e+07, 1200, 900, 9600, 55600, 6400, 1.2286e+06, 1.8708e+06, 58800, 15900, 16700, 1.0347e+06, 4.094e+05, 8.518e+05, 6400, 5200, 1600, 6300, 3000, 4300, 2500, 24500, 3300, 24000, 50100, 4.0289e+06, 1100, 100, 6700, 5200, 1.514e+05, 1.8027e+06, 1000, 12800, 1.291e+05 ]
},
"nodeWidth": 15,
"nodePadding": 10,
"layout": 32,
"id": "chart25446ad474c6"
};
params.units ? units = " " + params.units : units = "";
//hard code these now but eventually make available
var formatNumber = d3.format("0,.0f"), // zero decimal places
format = function(d) { return formatNumber(d) + units; },
color = d3.scale.category20();
var svg = d3.select('#' + params.id).append("svg")
.attr("width", params.width)
.attr("height", params.height);
var sankey = d3.sankey()
.nodeWidth(params.nodeWidth)
.nodePadding(params.nodePadding)
.layout(params.layout)
.size([params.width,params.height]);
var path = sankey.link();
var data = params.data,
links = [],
nodes = [];
//get all source and target into nodes
//will reduce to unique in the next step
//also get links in object form
data.source.forEach(function (d, i) {
nodes.push({ "name": data.source[i] });
nodes.push({ "name": data.target[i] });
links.push({ "source": data.source[i], "target": data.target[i], "value": +data.value[i] });
});
//now get nodes based on links data
//thanks Mike Bostock https://groups.google.com/d/msg/d3-js/pl297cFtIQk/Eso4q_eBu1IJ
//this handy little function returns only the distinct / unique nodes
nodes = d3.keys(d3.nest()
.key(function (d) { return d.name; })
.map(nodes));
//it appears d3 with force layout wants a numeric source and target
//so loop through each link replacing the text with its index from node
links.forEach(function (d, i) {
links[i].source = nodes.indexOf(links[i].source);
links[i].target = nodes.indexOf(links[i].target);
});
//now loop through each nodes to make nodes an array of objects rather than an array of strings
nodes.forEach(function (d, i) {
nodes[i] = { "name": d };
});
sankey
.nodes(nodes)
.links(links)
.layout(params.layout);
var link = svg.append("g").selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", function (d) { return Math.max(1, d.dy); })
.sort(function (a, b) { return b.dy - a.dy; });
link.append("title")
.text(function (d) { return d.source.name + " → " + d.target.name + "\n" + format(d.value); });
var node = svg.append("g").selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; })
.call(d3.behavior.drag()
.origin(function (d) { return d; })
.on("dragstart", function () { this.parentNode.appendChild(this); })
.on("drag", dragmove));
node.append("rect")
.attr("height", function (d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.style("fill", function (d) { return d.color = color(d.name.replace(/ .*/, "")); })
.style("stroke", function (d) { return d3.rgb(d.color).darker(2); })
.append("title")
.text(function (d) { return d.name + "\n" + format(d.value); });
node.append("text")
.attr("x", -6)
.attr("y", function (d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "end")
.attr("transform", null)
.text(function (d) { return d.name; })
.filter(function (d) { return d.x < params.width / 2; })
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start");
// the function for moving the nodes
function dragmove(d) {
d3.select(this).attr("transform",
"translate(" + (
d.x = Math.max(0, Math.min(params.width - d.dx, d3.event.x))
) + "," + (
d.y = Math.max(0, Math.min(params.height - d.dy, d3.event.y))
) + ")");
sankey.relayout();
link.attr("d", path);
}
</script>
</body>
<!-- Google Prettify -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/prettify/188.0.0/prettify.js"></script>
<script
src='https://google-code-prettify.googlecode.com/svn-history/r232/trunk/src/lang-r.js'>
</script>
<script>
var pres = document.getElementsByTagName("pre");
for (var i=0; i < pres.length; ++i) {
pres[i].className = "prettyprint linenums";
}
prettyPrint();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment