Skip to content

Instantly share code, notes, and snippets.

@njahn82
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njahn82/cc2bf0b39a247a8120e8 to your computer and use it in GitHub Desktop.
Save njahn82/cc2bf0b39a247a8120e8 to your computer and use it in GitHub Desktop.
unibiAPC
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<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-left: auto;
margin-right: auto;
width: 960px;
height: 700px;
}
</style>
</head>
<body >
<div id = 'chart35d5e18d0df' class = 'rChart rCharts_d3_sankey'></div>
<!--Attribution:
Mike Bostock https://github.com/d3/d3-plugins/tree/master/sankey
Mike Bostock http://bost.ocks.org/mike/sankey/
-->
<script>
(function(){
var params = {
"dom": "chart35d5e18d0df",
"width": 960,
"height": 700,
"data": {
"source": [ "Association for Research in Vision and Ophthalmology (ARVO)", "Association for Research in Vision and Ophthalmology (ARVO)", "Association for Research in Vision and Ophthalmology (ARVO)", "Elsevier BV", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Frontiers Media SA", "Hindawi Publishing Corporation", "Hindawi Publishing Corporation", "Hindawi Publishing Corporation", "Informa UK Limited", "MDPI AG", "MDPI AG", "MDPI AG", "MDPI AG", "Nature Publishing Group", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Public Library of Science (PLoS)", "Scientific Research Publishing, Inc,", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Springer Science + Business Media", "Wiley-Blackwell", "Wiley-Blackwell", "Wiley-Blackwell" ],
"target": [ "Journal of Vision", "Journal of Vision", "Journal of Vision", "CSBJ", "Frontiers in Cellular and Infection Microbiology", "Frontiers in Neural Circuits", "Front. Psychology", "Front. Psychology", "Front. Psychology", "Front. Plant Sci.", "Front. Psychology", "Front. Neurorobot.", "Frontiers in Psychology", "Frontiers in Behavioral Neuroscience", "Front. Plant Sci.", "Frontiers in Behavioral Neuroscience", "Front. Hum. Neurosci.", "Frontiers in Psychology", "Frontiers in Psychology", "Front. Hum. Neurosci.", "Frontiers in Behavioral Neuroscience", "Frontiers in Psychology", "Front. Comput. Neurosci.", "Front. Comput. Neurosci.", "Front. Plant Sci.", "Frontiers in Behavioral Neuroscience", "Frontiers in Behavioral Neuroscience", "Anatomy Research International", "Education Research International", "Stem Cells International", "Health Psychology and Behavioral Medicine", "Entropy", "Symmetry", "Symmetry", "Entropy", "Scientific Reports", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS Computational Biology", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "PLoS ONE", "Journal of Intelligent Learning Systems and Applications", "BMC Microbiol", "BMC Genomics", "BMC Bioinformatics", "BMC Bioinformatics", "BMC Bioinformatics", "SpringerPlus", "BMC Public Health", "BMC Structural Biology", "BMC Microbiol", "BMC Genomics", "BMC Public Health", "BMC Microbiol", "Health and Quality of Life Outcomes", "BMC Genomics", "BMC Genomics", "BMC Pregnancy and Childbirth", "BMC Public Health", "BMC Biotechnology", "BMC Public Health", "Journal of NeuroEngineering and Rehabilitation", "Microb Cell Fact", "Biotechnol Biofuels", "BMC Psychol", "BMC Bioinformatics", "BMC Bioinformatics", "BMC Public Health", "BMC Genomics", "BMC Genomics", "Globalization and Health", "BMC Genomics", "BMC Public Health", "BMC Biotechnology", "BMC Genomics", "Ecol Evol", "Ecol Evol", "Ecol Evol" ],
"value": [ 961, 1037, 1724, 142, 900, 1600, 960, 960, 960, 960, 960, 1142, 1142, 1904, 1142, 916, 1143, 1142, 917, 1904, 1142, 1142, 1143, 1142, 1142, 1904, 1142, 388, 462, 1078, 1017, 990, 493, 493, 1175, 1046, 915, 968, 958, 968, 1265, 1109, 2053, 1109, 1232, 1109, 1232, 1232, 1232, 1232, 1069, 1232, 1109, 1109, 1069, 1069, 732, 1370, 1500, 1257.5, 1257.5, 1520, 850, 1490, 1475, 1545, 1480, 1470, 1490, 1565, 1190, 1535, 1545, 1252, 1232, 1228, 948, 1048, 1348, 1236, 1081, 1081, 1296, 1212, 1228, 1216, 1212, 1236, 1296, 1212, 1333, 1457, 1457 ]
},
"nodeWidth": 15,
"nodePadding": 10,
"layout": 32,
"unit": "EURO",
"id": "chart35d5e18d0df"
};
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();
if(params.labelFormat){
formatNumber = d3.format(".2%");
}
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>
<script></script>
</body>
</html>
require(devtools)
install_github('rCharts', 'ramnathv')
unibi <- read.csv("https://github.com/njahn82/unibiAPC/blob/master/data/unibi12-13.csv", header =T, sep=",")
#select columns
unibi.sub <- unibi[,c("Publisher", "Journal", "EURO")]
#rename
colnames(unibi.sub) <- c("source", "target", "value")
unibi.sub$value <- as.numeric(unibi.sub$value)
#now we finally have the data in the form we need
sankeyPlot <- rCharts$new()
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot$set(
data = unibi.sub,
nodeWidth = 15,
nodePadding = 10,
layout = 32,
width = 960,
height = 700,
unit = "EURO"
)
sankeyPlot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment