Published
Edited
Apr 6, 2022
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
byIndividual = d3.tsv(
"https://gist.githubusercontent.com/Mbrownshoes/e5b5bf4f9767d6762db3848d9fac749e/raw/73e0583e2cec2c888c9d07466e58f6e95c234930/OTU_table.tsv"
)
Insert cell
byIndividual.columns
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
byIndividual[0]
Insert cell
// convert wide to long
longData = {
// helpful http://jonathansoma.com/tutorials/d3/wide-vs-long-data/
let long_data = [];

byIndividual.forEach(row => {
// Loop through all of the columns, and for each column
// make a new row
Object.keys(row).forEach(function(colname) {
// Ignore 'State' and 'Value' columns
if (colname == "#OTU ID" || colname == "taxonomy") {
return;
}
let heir = row["taxonomy"].split(";");
long_data.push({
Indv: row["#OTU ID"],
Value: row[colname],
Sample: colname,
kingdom: heir[0].replace(' ', ''),
phylum: heir[1].replace(' ', ''),
class: heir[2].replace(' ', ''),
order: heir[3].replace(' ', ''),
family: heir[4].replace(' ', ''),
genus: heir[5].replace(' ', ''),
species: heir[6].replace(' ', ''),
taxonomy: row["taxonomy"]
});
});
// return row;
});
return long_data;
}
Insert cell
Insert cell
Insert cell
Insert cell
longData
Insert cell
fakeSites
Insert cell
joinedDataFake = {
let c = _.cloneDeep(longData);
c.forEach(function(individual) {
var result = fakeSites.filter(function(sample) {
return sample.SampleID === individual.Sample;
});
// delete article.brand_id;
// console.log(result[0]);
individual.month =
result[0] !== undefined ? result[0].Collection_month : null;
individual.site = result[0] !== undefined ? result[0].ARMS_number : null;
});
// console.log(c.filter(d => d.phylum === " p__Mollusca"));
return c.filter(
d =>
d.site === map &&
d.phylum === "p__Mollusca" &&
d.order !== "o__Neoloricata" &&
d.order !== "o__undef_Gastropoda"
);
// c.age = +c.age;
// c.salary = +c.salary;
return c.sort(function(a,b) { return a.month.toLowerCase().localeCompare(b.month.toLowerCase()) });
}
Insert cell
individual
Insert cell
Insert cell
Insert cell
chartData = {
const sums = d3
.nest()
// .key(d => d.site)
.key(d => d.month)
.key(d => d.species.replace("s___", "").replace("s__", ""))
.rollup(v => d3.sum(v, d => d.Value))
.object(joinedDataFake);
// return sums;

let newDat = [];
Object.keys(sums).forEach(d => {
// console.log(sums[d]);
sums[d].month = d;
newDat.push(sums[d]);
});
newDat["columns"] = speciesListFake;

let t;
let i;
newDat.forEach(d => {
for (i = 1, t = 0; i < speciesListFake.length; ++i)
t += d[speciesListFake[i]] = +d[speciesListFake[i]];
d.total = t;
});
return newDat.sort(function(a, b) {
return a.month.toLowerCase().localeCompare(b.month.toLowerCase());
});
}
Insert cell
d3
.nest()
// .key(d => d.site)
.key(d => d.month)
.key(d => d.species.replace("s___", "").replace("s__", ""))
.rollup(v => d3.sum(v, d => d.Value))
.object(joinedDataFake)
Insert cell
joinedDataFake[0]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
groupSum(longData, d => d.taxonomy, d => Number(d.Value))
Insert cell
nest(longData, d => d.Sample, d => d.taxonomy)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
groupSum(byIndividual, d => d.taxonomy, d => d["ARMS9-M-106"])
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// import {heightA,heightB,heightC,widthA,widthB, widthC} from '@mbostock/dashboard'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {select} from '@jashkenas/inputs'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// vegalite = require("@observablehq/vega-lite@0.0")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more