Public
Edited
Jul 31, 2023
Insert cell
Insert cell
Insert cell
Insert cell
crabs = FileAttachment("fiddlerCrabBodySize.csv").csv({typed: true})
// ^ using typed: true parses the values; otherwise FileAttachment would assume they're all strings
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Access Adélie data here:
adelie = d3.csv("https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.219.5&entityid=002f3893385f710df69eeebe893144ff", d3.autoType)
Insert cell
// Access chinstrap data here:
chinstrap = d3.csv("https://portal.edirepository.org/nis/dataviewer?packageid=knb-lter-pal.221.8&entityid=fe853aa8f7a59aa84cdd3197619ef462", d3.autoType)

Insert cell
Insert cell
Insert cell
// Make combined version, penguinsCombo, here
// this is like a row-bind? function in R or Python
penguinsCombo = adelie.concat(gentoo, chinstrap)

Insert cell
Insert cell
Insert cell
// Create the wrangled version of penguins here:
penguins = penguinsCombo.map(({
Species: species,
Island: island,
Sex: sex,
"Culmen Length (mm)": bill_length_mm,
"Culmen Depth (mm)": bill_depth_mm,
"Body Mass (g)": body_mass_g,
"Flipper Length (mm)": flipper_length_mm
}) => ({
species: species.split(" ")[0],
island,
sex: sex == null || sex == "." ? null : sex.toLowerCase(),
bill_length_mm,
bill_depth_mm,
body_mass_g,
flipper_length_mm
}))
Insert cell
Insert cell
// penguins = penguinsKeyCopy
Insert cell
Insert cell
import {chart} from "@d3/zoomable-sunburst"
Insert cell
Insert cell
import {aq, op} from "@uwdata/arquero"
Insert cell
Insert cell
// Convert your array of objects to an Arquero table here:
penguinsTable = aq.from(penguins)
Insert cell
penguinsTableView = penguinsTable.view()
Insert cell
Insert cell
// Write Arquero code to perform the steps above here:
penguinsTable
.filter((d) => d.sex == "female")
.select("species", "bill_depth_mm", "bill_length_mm")
.derive({bill_ratio: d => d.bill_length_mm / d.bill_depth_mm})
.groupby("species")
.rollup({mean_bill_ratio: d => op.mean(d.bill_ratio)}) // summarze
.view()
Insert cell
Insert cell
Insert cell
Insert cell
penguins
X
bill_length_mm
Y
bill_depth_mm
Color
species
Size
Facet X
Facet Y
Mark
Auto
Type Chart, then Shift-Enter. Ctrl-space for more options.

Insert cell
Plot.plot({
color: { legend: true },
marks: [
Plot.dot(penguins, {
x: "bill_length_mm",
y: "bill_depth_mm",
fill: "species",
tip: true,
r: "body_mass_g",
opacity: 0.5,
}),
Plot.frame(),
Plot.linearRegressionX({x: "bill_length_mm", y: "bill_depth_mm"})
],
color: {range: ["teal", "darkorange", "orchid" ]},
r: { domain: d3.extent(penguins.map(({body_mass_g}) => body_mass_g)), range: [1, 10]}
})
Insert cell
Insert cell
Insert cell
import {PlotMatrix} with {data} from "@observablehq/autoplot-matrix"
Insert cell
Insert cell
// Make a copy of penguins here, stored as data:
data = penguins
Insert cell
Insert cell
// Make the pairplot with PlotMatrix here:
PlotMatrix(data)
Insert cell
Insert cell
Insert cell
penguins
X
body_mass_g
Y
Color
species
Size
Facet X
Facet Y
species
Mark
Auto
Type Chart, then Shift-Enter. Ctrl-space for more options.

Insert cell
viewof chooseVariable = Inputs.radio(
["bill_depth_mm", "bill_length_mm", "flipper_length_mm", "body_mass_g"],
{ label: "Select one", value: "body_mass_g" }
)
Insert cell
Plot.plot({
color: { legend: true },
marks: [
Plot.frame({ strokeOpacity: 0.1 }),
Plot.rectY(
penguins,
Plot.binX(
{ y: "count" },
{ fy: "species", x: chooseVariable, fill: "species", tip: true }
)
),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {scale} from "@chrispahm/hierarchical-clustering"
Insert cell
// Make a subset of penguins with complete cases (filter out values where bill length is null):
penguinsComplete = penguins.filter(({bill_length_mm, sex}) => bill_length_mm !== null) // && sex == "female")

Insert cell
// Create a scaled version of the values (non-numeric will be NaN, which is fine..):
penguinsScale = scale(penguinsComplete )
Insert cell
// Convert the array of objects to an array of arrays:
penguinsArray = penguinsScale.map(
({bill_length_mm, bill_depth_mm, body_mass_g, flipper_length_mm}) => [
bill_length_mm, bill_depth_mm, body_mass_g, flipper_length_mm
]
)
Insert cell
Insert cell
Insert cell
// Use ml.js KMeans() method to perform k-means clustering for k centroids:
penguinsClusters = ML.KMeans(penguinsArray, 3)
Insert cell
penguinsClusters.clusters
Insert cell
Insert cell
penguinsKmeans = penguinsComplete.map((d, i) => ({...d, clusterNo: penguinsClusters.clusters[i]}))
Insert cell
penguinsKmeans
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
kmeanschart = Plot.plot({
marks: [
Plot.text(penguinsKmeans, {
x: "body_mass_g",
y: "flipper_length_mm",
text: "clusterNo",
fontSize: "15px",
fontWeight: "500",
fill: "species",
tip: true
})
],
color: {legend: true}
})
Insert cell
clusterCounts = d3.rollup(penguinsKmeans, ({length}) => length, ({species}) => species, ({clusterNo}) => clusterNo)
Insert cell
Insert cell
Insert cell
ML = require("https://www.lactame.com/lib/ml/6.0.0/ml.min.js")
Insert cell
import {penguinsKeyCopy} from "@observablehq/ds-workflows-in-js-session-2-key"
Insert cell
noUse = FileAttachment("fiddlerCrabBodySize.csv") // Note: this is only added here so that the file is attached in the forked version
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