Public
Edited
Apr 27, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
fishing = [{river: "San Juan", species: "rainbow", lengthInches: 18.2},
{river: "San Juan", species: "brown", lengthInches: 14.1},
{river: "San Juan", species: "brown", lengthInches: 21.5},
{river: "Rio Grande", species: "brown", lengthInches: 9.0},
{river: "Rio Grande", species: "brown", lengthInches: 12.5},
{river: "Rio Grande", species: "cutthroat", lengthInches: 10.6},
{river: "Rio Grande", species: "brown", lengthInches: 14.5},
{river: "Rio Chama", species: "rainbow", lengthInches: 8.5},
{river: "Rio Chama", species: "rainbow", lengthInches: 11.5}]
Insert cell
Insert cell
Insert cell
fishing
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
//Group the data by species, then get an array of objects for rainbow trout.

Insert cell
Insert cell
Insert cell
// Use Array.filter to get a subset only for rainbow trout in fishing:

Insert cell
Insert cell
// Find the mean trout length by river:
// option+shift+f = prettify code formatting
riverTroutSize = d3.rollup(
fishing,
(v) => d3.mean(v, (d) => d.lengthInches),
(d) => d.river
)
Insert cell
Insert cell
// Get just the mean trout size for the San Juan River:
riverTroutSize.get("San Juan")
Insert cell
Insert cell
Insert cell
Insert cell
// Load the us_imports.csv file (attached in this notebook):
// typed means it is being parsed
us_imports = FileAttachment("us_imports.csv").csv({typed: true})
Insert cell
us_imports
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
Insert cell
// Return an array of all values in the 'value' property of us_imports, log transformed
// exponent ^ in javascript is ** character
us_imports.map(d => Math.log(d.value))
Insert cell
Insert cell
Insert cell
// Create a new array, usLogValue, containing all existing properties and ADDING the log transformed value as a property named logValue:
// spread operator ... says keep everything and add 1 more
usLogValue = us_imports.map(d => ({...d, logValue: Math.log(d.value)}))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.rectY(usLogValue, Plot.binX({y: "count"}, {x: "logValue"})),
Plot.ruleY([0])
]
})
// use bins if it is numerical value
// usse groups if it is categorical value
Insert cell
Plot.plot({
marks: [
Plot.rectY(us_imports, Plot.binX({y: "count"}, {x: d => Math.log(d.value)})),
Plot.ruleY([0])
]
})
// can add, thresholds or intervals to modify bins
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// just released 2 weeks ago

Insert cell
usLogValue
X
sum
value
Y
Sector
Color
Sector
Size
Facet X
year
Facet Y
Mark
Auto
Type Chart, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// example of slider called height
viewof height = Inputs.range([400, 1200], {label: "Chart Height:", step: 1})
Insert cell
viewof radios = Inputs.radio(["A", "B"], {label: "Select one", value: "A"})
Insert cell
viewof pickSector = Inputs.radio(us_imports.map((d) => d.Sector), {
label: "Select one",
value: "A",
unique: true, // ensures each only appears once
})
Insert cell
pickSector
Insert cell
Insert cell
// new code with conditional fill
Plot.plot({
marks: [
Plot.barX(
us_imports,
Plot.groupY(
{ x: "sum" },
{
x: "value",
y: "Sector",
fill: d => d.Sector === pickSector ? "red" : "gray", // add conditional fill statement
sort: { y: "x", reverse: true }
}
)
)
],
x: { tickFormat: ".1s" },
width: 900
})
Insert cell
viewof pickColor = Inputs.color({label: "Pick Favorite background color", value: "#4682b4"})
Insert cell
pickColor
Insert cell
// new code with conditional fill
Plot.plot({
marks: [
Plot.barX(
us_imports,
Plot.groupY(
{ x: "sum" },
{
x: "value",
y: "Sector",
fill: d => d.Sector === pickSector ? "red" : pickColor, // add conditional fill statement
sort: { y: "x", reverse: true }
}
)
)
],
x: { tickFormat: ".1s" },
width: 900
})
Insert cell
import {Plot} from "@mkfreeman/plot-tooltip"

Insert cell
import { showMe } from "@observablehq/show-me"
Insert cell
import { addTooltips } from "@mkfreeman/plot-tooltip"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more