Skip to content

Instantly share code, notes, and snippets.

@fogonwater
Last active March 18, 2018 22:01
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 fogonwater/871f153d225a4cf41a250394d6d51994 to your computer and use it in GitHub Desktop.
Save fogonwater/871f153d225a4cf41a250394d6d51994 to your computer and use it in GitHub Desktop.
Building a radial hierarchy from a flat table
license: mit
height: 1060
border: no
kingdom phylum_latin phylum_common taxon described known_undescribed estimated_unknown adventive endemic_species endemic_genera notes
Plantae Glaucophyta glaucophytes Glaucophyta 1 0 1 0 0 0
Plantae Rhodophyta red algae Cyanidiophyceae 1 0 0 0 0 0
Plantae Rhodophyta red algae Compsopogonophyceae 10 0 0 0 5 2
Plantae Rhodophyta red algae Porphyridiophyceae 1 0 0 0 0 0
Plantae Rhodophyta red algae Stylonematophyceae 2 0 0 0 0 0
Plantae Rhodophyta red algae Bangiophyceae 11 36 5 0 7 3
Plantae Rhodophyta red algae Florideophyceae 447 53 30 15 178 16
Plantae Chlorophyta green algae Mamiellophyceae 3 0 30 0 0 0
Plantae Chlorophyta green algae Nephroselmidophyceae 2 0 15 0 0 0
Plantae Chlorophyta green algae Pyramimonadophyceae 11 2 30 0 0 0
Plantae Chlorophyta green algae Chlorodendrophyceae 1 0 5 0 0 0
Plantae Chlorophyta green algae Chlorophyceae 287 4 350 1 4 0
Plantae Chlorophyta green algae Ulvophyceae 181 11 60 9 24 2
Plantae Chlorophyta green algae Trebouxiophyceae 81 2 120 0 1 0
Plantae Charophyta green algae Klebsormidiophyceae 9 0 3 0 1 0
Plantae Charophyta green algae Zygnematophyceae 496 5 50 0 39 0
Plantae Charophyta green algae Coleochaetophyceae 6 0 2 0 0 0
Plantae Charophyta green algae Charophyceae 16 2 5 1 3 0
Plantae Bryophyta hornworts, liverworts, mosses Anthocerotophyta 13 0 2 0 8 0 hornworts
Plantae Bryophyta hornworts, liverworts, mosses Marchantiophyta 593 0 50 6 276 14 liverworts
Plantae Bryophyta hornworts, liverworts, mosses Bryopsida 516 0 10 30 110 9 mosses
Plantae Tracheophyta vascular plants Lycopodiopsida 16 0 0 3 2 0 clubmosses and firmosses
Plantae Tracheophyta vascular plants Psilotopsida 11 0 0 0 3 0 a class of ferns
Plantae Tracheophyta vascular plants Equisetopsida 3 0 0 3 0 0 a class of ferns
Plantae Tracheophyta vascular plants Marattiopsida 1 0 0 0 0 0 a class of ferns
Plantae Tracheophyta vascular plants Polypodiopsida 216 1 7 46 82 3 a class of ferns
Plantae Tracheophyta vascular plants Ginkgoopsida 1 0 0 1 0 0 gingko
Plantae Tracheophyta vascular plants Pinopsida 60 0 0 40 20 2 confiers
Plantae Tracheophyta vascular plants Magnoliopsida 4436 0 350 2430 1628 48 a class of flowering plants
Fungi Chytridiomycota chytrids Blastocladiomycota 12 1 15 3 0 0
Fungi Chytridiomycota chytrids Chytridiomycota 133 0 150 9 7 0
Fungi Chytridiomycota chytrids Neocallimastigomycota 5 0 10 1 0 0
Fungi Microsporidia spore-forming parasites Microsporidia 12 6 9000 6 5 1
Fungi Zygomyota pin moulds Zygomyota 89 0 170 19 10 0
Fungi Glomeromycota arbuscular mycorrhiza Glomeromycota 38 1 5 2 0 0
Fungi Ascomycota sac fungi Incertae sedis 296 7 2500 61 65 2
Fungi Ascomycota sac fungi Arthoniomycetes 102 1 100 0 34 0
Fungi Ascomycota sac fungi Dothideomycetes 1155 10 2500 660 157 1
Fungi Ascomycota sac fungi Eurotiomycetes 287 2 500 51 16 0
Fungi Ascomycota sac fungi Laboulbeniomycetes 33 4 200 0 25 1
Fungi Ascomycota sac fungi Lecanoromycetes 1406 3 100 2 162 1
Fungi Ascomycota sac fungi Leotiomycetes 509 24 500 177 138 5
Fungi Ascomycota sac fungi Lichinomycetes 11 0 10 0 0 0
Fungi Ascomycota sac fungi Orbiliomycetes 22 0 20 8 1 0
Fungi Ascomycota sac fungi Pezizomycetes 167 1 20 78 19 1
Fungi Ascomycota sac fungi Sordariomycetes 1165 27 2500 416 163 2
Fungi Ascomycota sac fungi Saccharomycetes 77 0 100 26 0 0
Fungi Ascomycota sac fungi Pneumocystidomycetes 1 0 5 1 0 0
Fungi Ascomycota sac fungi Schizosaccharomycetes 1 0 5 0 0 0
Fungi Ascomycota sac fungi Taphrinomycetes 6 0 5 5 0 0
Fungi Basidiomycota mushrooms, rusts, smuts Agaricomycetes 2030 259 5000 282 740 6
Fungi Basidiomycota mushrooms, rusts, smuts Dacrymycetes 27 0 20 0 4 0
Fungi Basidiomycota mushrooms, rusts, smuts Tremellomycetes 49 0 20 9 7 0
Fungi Basidiomycota mushrooms, rusts, smuts Entorhizomycetes 7 0 5 1 2 0
Fungi Basidiomycota mushrooms, rusts, smuts Agaricostilbomycetes 6 0 5 0 1 0
Fungi Basidiomycota mushrooms, rusts, smuts Atractiellomycetes 5 0 5 0 0 0
Fungi Basidiomycota mushrooms, rusts, smuts Classiculomycetes 1 0 5 0 0 0
Fungi Basidiomycota mushrooms, rusts, smuts Microbotryomycetes 30 0 20 6 0 0
Fungi Basidiomycota mushrooms, rusts, smuts Pucciniomycetes 253 2 15 121 91 0
Fungi Basidiomycota mushrooms, rusts, smuts Exobasidiomycetes 51 0 5 37 5 0
Fungi Basidiomycota mushrooms, rusts, smuts Ustilaginomycetes 56 2 10 25 11 0
Bacteria Cyanobacteria blue-green bacteria and algae Cyanobacteria 372 30 200 1 19 1
Protozoa Euglenozoa euglenoids, bodonoids, trypanosomes Euglenoidea 98 2 50 0 1 0
Protozoa Euglenozoa euglenoids, bodonoids, trypanosomes Kinetoplastea 10 5 50 1 7 0
Protozoa Percolozoa acrasid slime molds Percolozoa 7 6 8 0 1 0
Protozoa Loukozoa excavate eukaryotes Loukozoa 1 0 3 0 0 0
Protozoa Metamonada oxymonads, parabasalids, Giardia and kin Anaeromonadea 1 0 6 0 1 0
Protozoa Metamonada oxymonads, parabasalids, Giardia and kin Trichomonadea 10 3 45 7 3 0
Protozoa Metamonada oxymonads, parabasalids, Giardia and kin Trichonymphea 2 0 4 0 2 1
Protozoa Metamonada oxymonads, parabasalids, Giardia and kin Trepomonadea 1 0 2 1 0 0
Protozoa Metamonada oxymonads, parabasalids, Giardia and kin Retortamonadea 1 0 3 1 0 0
Protozoa Amoebozoa amoebas, pelobionts, slime moulds and kin Archamoebae 6 1 20 6 0 0
Protozoa Amoebozoa amoebas, pelobionts, slime moulds and kin Variosea 5 2 10 6 1 1
Protozoa Amoebozoa amoebas, pelobionts, slime moulds and kin Protostelea 23 0 5 0 0 0
Protozoa Amoebozoa amoebas, pelobionts, slime moulds and kin Dictyostelea 14 0 5 0 3 0
Protozoa Amoebozoa amoebas, pelobionts, slime moulds and kin Myxogastrea 197 0 5 0 0 0
Protozoa Amoebozoa amoebas, pelobionts, slime moulds and kin Tubulinea 87 7 330 1 2 0
Protozoa Amoebozoa amoebas, pelobionts, slime moulds and kin Flabellinia 6 1 20 0 0 0
Protozoa Apusozoa thecomonad, ancyromonad diphylleid zooflagellates Apusozoa 1 0 3 0 1 0
Protozoa Choanozoa collar flagellates, nucleariids and kin Cristidiscoidea 7 0 5 0 0 0
Protozoa Choanozoa collar flagellates, nucleariids and kin Choanoflagellatea 26 5 20 0 0 1
Protozoa Choanozoa collar flagellates, nucleariids and kin Ichthyosporea 4 0 20 0 2 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Eustigmatophyceae 7 1 5 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Chrysophyceae 101 4 200 0 7 0 I suspect the endemic entry is missing from p. 155 of NZIB
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Synchromophyceae 0 0 1 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Picophagophyceae 0 0 4 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Pelagophyceae 0 0 4 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Dictyochophyceae 8 0 5 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Pinguiophyceae 0 0 4 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Raphidophyceae 4 0 5 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Chrysomerophyceae 1 0 0 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Aurearenophyceae 0 0 1 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Xanthophyceae 45 1 10 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Phaeothamniophyceae 2 1 0 0 0 0
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Phaeophyceae 145 3 20 12 50 4
Chromista Ochrophyta brown and golden algae, diatoms, silicoflagellates and kin Bacillariophyceae 1452 20 1000 2 5 1
Chromista Oomycota water moulds, downy mildews Hyphochytrea 7 0 20 0 0 0
Chromista Oomycota water moulds, downy mildews Peronospora 144 10 50 106 1 0
Chromista Bigyra opalinids, slime nets, thraustochytrids Nucleohelea 1 0 3 0 0 0
Chromista Bigyra opalinids, slime nets, thraustochytrids Proteromonadidae 0 0 2 0 0 0
Chromista Bigyra opalinids, slime nets, thraustochytrids Opalinata 2 0 2 2 0 0
Chromista Bigyra opalinids, slime nets, thraustochytrids Blastocystea 1 0 2 1 0 0
Chromista Bigyra opalinids, slime nets, thraustochytrids Bicoecea 2 1 20 0 0 0
Chromista Bigyra opalinids, slime nets, thraustochytrids Labyrinthulea 8 0 6 0 0 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Colponemea 1 0 2 0 0 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Myzomonadea 0 0 3 0 0 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Perkinsea 1 0 4 0 0 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Ellobiopsida 2 0 10 0 0 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Syndinea 0 0 15 0 0 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Noctilucea 2 0 1 0 0 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Peridinea 265 11 220 0 0 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Apicomonadea 0 0 5 0 0 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Coccidia 100 28 100 111 12 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Gregarinea 4 13 1000 1 16 0
Chromista Myzozoa dinoflagellates, perkinsids, ellobiopsids, sporozoans Haemotozoa 9 5 10 6 4 0
Chromista Ciliophora ciliates Postciliodesmatophora 14 2 100 0 4 0
Chromista Ciliophora ciliates Intramacronucleata 326 5 1000 37 46 0
Chromista Cercozoa single-celled eukaryotes Chlorarachnea 0 0 2 0 0 0
Chromista Cercozoa single-celled eukaryotes Granofilosea 1 0 20 0 0 0
Chromista Cercozoa single-celled eukaryotes Sarcomonadea 5 0 50 0 2 0
Chromista Cercozoa single-celled eukaryotes Thecofilosea 18 1 14 0 0 0
Chromista Cercozoa single-celled eukaryotes Imbricatea 38 0 22 0 0 0
Chromista Cercozoa single-celled eukaryotes Proteomyxidea 1 0 7 0 0 0
Chromista Cercozoa single-celled eukaryotes Phytomyxea 9 0 2 9 0 0
Chromista Cercozoa single-celled eukaryotes Gromiidea 1 0 1 0 0 0
Chromista Cercozoa single-celled eukaryotes Ascetosporea 2 2 5 0 4 0
Chromista Cercozoa single-celled eukaryotes Incertae sedis 5 0 8 0 4 0 Combines two Incertae Sedis from different sub-phylum
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<meta charset="utf-8">
<style>
body {text-align:center}
svg { font-family: Courier;}
.node circle {
fill: #999;
}
.node text {
font: 10px sans-serif;
}
.node--internal circle {
fill: #555;
stroke: #222;
}
.node--internal text {
text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff;
}
.link {
fill: none;
stroke: #555;
stroke-opacity: 0.4;
stroke-width: 1.5px;
}
</style>
<body>
<svg width="1060" height="1060"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
const svg = d3.select("svg"),
width = +svg.attr("width"),
height= +svg.attr("height"),
margin= 20,
max_radius = 20,
max_value = 4000,
g = svg.append("g").attr("transform", "translate(" + (width / 2 + 40) + "," + (height / 2 + 90) + ")"),
// Plantae, Fungi, Protozoa,Chromista
kingdom = 'Plantae'
// Declare a D3 layout
const tree = d3.tree()
.size([2 * Math.PI, 400])
.separation(function(a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth; })
// Load our external data
d3.csv("data.csv", function(data) {
//var data = data.filter( (d) => d.kingdom == kingdom)
data.sort(function(x, y){return d3.descending(+x.described, +y.described)})
var fineData = []
data.forEach(function(d){
if (+d.adventive > 0) {
var item = Object.assign({}, d)
item['type'] = 'adventive'
item['itemCount'] = +d.adventive
fineData.push(item)
}
if ((+d.described - +d.adventive - +d.endemic_species) > 0) {
var item = Object.assign({}, d)
item['type'] = 'native'
item['itemCount'] = +d.described - +d.adventive - +d.endemic_species
fineData.push(item)
}
if (+d.endemic_species > 0) {
var item = Object.assign({}, d)
item['type'] = 'endemic'
item['itemCount'] = +d.endemic_species
fineData.push(item)
}
})
// Build our hierarchy from a series of levels
// change/reorder the level elements for a different tree
const levels = ["kingdom", "phylum_common", "taxon"],
root = flatToHierarchy(fineData, levels, 'type', 'itemCount')
// Draw on screen
var link = g.selectAll(".link")
.data(tree(root).links())
.enter().append("path")
.attr("class", "link")
.attr("d", d3.linkRadial()
.angle(function(d) { return d.x; })
.radius(function(d) { return d.y; }))
var node = g.selectAll(".node")
.data(tree(root).descendants())
.enter().append("g")
//.attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); })
.style("fill", function(d) {
if (d.children) {return '#888'}
//console.log(d.data.type)
if (d.data.type == 'adventive') {return 'red'}
if (d.data.type == 'native') {return 'blue'}
if (d.data.type == 'endemic') {return 'green'}
})
.attr("transform", function(d) { return "translate(" + radialPoint(d.x, d.y) + ")"; })
node.append("circle")
.attr('r', (d) => radius(d.value))
/*
node.append("text")
.attr("dy", "0.31em")
.attr("x", function(d) { return d.x < Math.PI === !d.children ? 6 : -6; })
.attr("text-anchor", function(d) { return d.x < Math.PI === !d.children ? "start" : "end"; })
.attr("transform", function(d) { return "rotate(" + (d.x < Math.PI ? d.x - Math.PI / 2 : d.x + Math.PI / 2) * 180 / Math.PI + ")"; })
.text(function(d) { return d.children ? d.data.name : ''})*/
})
//
//
// Helper function for converting flat data to a hierarchy
// with a name and count field on each node.
// TODO - probably shouldn't name the value field "count"
function radius(val) {
return Math.sqrt(val / max_value) * max_radius
}
function radialPoint(x, y) {
return [(y = +y) * Math.cos(x -= Math.PI / 2), y * Math.sin(x)];
}
function flatToHierarchy(flatData, levels, nameField, countField) {
// Adapted from https://stackoverflow.com/a/19317823
var nestedData = { name :"root", children : [] }
// For each data row, loop through the expected levels traversing the output tree
flatData.forEach(function(d){
// Keep this as a reference to the current level
var depthCursor = nestedData.children;
// Go down one level at a time
levels.forEach(function( property, depth ){
// See if a branch has already been created
var index;
depthCursor.forEach(function(child,i){
if ( d[property] == child.name ) index = i;
});
// Add a branch if it isn't there
if ( isNaN(index) ) {
depthCursor.push({ name : d[property], children : []});
index = depthCursor.length - 1;
}
// Reference the new child array as we go deeper into the tree
depthCursor = depthCursor[index].children;
// This is a leaf, so add last element to specified branch
if ( depth === levels.length - 1 ) {
depthCursor.push({
'name':d[nameField],
'count':+d[countField],
'type':d.type /// TODO - generic way to add fields
});
}
})
})
// sum up the leaves / branches and return the hierarchy
return d3.hierarchy(nestedData).sum(function(d){ return d.count; })
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment