Public
Edited
Sep 20, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
dataURL = 'https://data.ny.gov/api/views/vxuj-8kew/rows.csv?accessType=DOWNLOAD&sorting=true'
Insert cell
rawMtaData = d3.csv('https://data.ny.gov/api/views/vxuj-8kew/rows.csv?accessType=DOWNLOAD&sorting=true', d3.autoType)
Insert cell
columns = [
["Subways: Total Estimated Ridership", "Subways: % of Comparable Pre-Pandemic Day"],
["Buses: Total Estimated Ridership", "Buses: % of Comparable Pre-Pandemic Day"],
["LIRR: Total Estimated Ridership","LIRR: % of 2019 Monthly Weekday/Saturday/Sunday Average"],
["Metro-North: Total Estimated Ridership","Metro-North: % of 2019 Monthly Weekday/Saturday/Sunday Average"],
["Access-A-Ride: Total Scheduled Trips","Access-A-Ride: % of Comprable Pre-Pandemic Day"],
["Bridges and Tunnels: Total Traffic","Bridges and Tunnels: % of Comparable Pre-Pandemic Day"],
]
Insert cell
parseDate = d3.timeParse('%m/%d/%Y')
Insert cell
yearFormat = d3.timeFormat('%Y')
Insert cell
dayOfWeekFormat = d3.timeFormat('%A')
Insert cell
mtaData = rawMtaData.flatMap(d => {
const date = parseDate(d.Date);
return columns.map(c => {
return {
'type': c[0].split(":")[0],
'date': date,
'year': yearFormat(date),
'dayOfWeek': dayOfWeekFormat(date),
'value': d[c[0]],
'percent': d[c[1]]
}
})
})
Insert cell
movingN
Insert cell
new Set(mtaData.map(d => d.type))
Insert cell
d3.group(mtaData, d => d.type)
Insert cell
Insert cell
viewof selectMTAData = Inputs.select(d3.group(mtaData, d => d.type), {label: "Select one"})
Insert cell
viewof selectMTAData2 = Inputs.select(Array.from(new Set(mtaData.map(d => d.type))).concat('All'), {label: "Select one"})
Insert cell
Plot.plot({
marginLeft: 100,
width: width,
marks: [
Plot.ruleY([0]),
Plot.lineY(selectMTAData, Plot.windowY({
reduce: 'p75',
k: movingN,
x: "date",
y: "value",
stroke: "type",
// mixBlendMode: "multiply"
}))
],
color: {
legend: true
}
})
Insert cell
Insert cell
Plot.plot({
marginLeft: 100,
height: 600,
width: width,
facet: {
data: mtaData,
y: 'type',
marginRight: 150,
},
marks: [
Plot.ruleY([0]),
Plot.lineY(mtaData, Plot.normalizeY({
basis: 'max',
x: "date",
y: "value",
stroke: "type",
title: d => `${d.date} - ${d.value}`
})),
],
color: {
legend: true,
scheme: 'dark2'
}
})
Insert cell
Plot.plot({
marginLeft: 100,
// height: 600,
width: width,
marks: [
Plot.ruleY([0]),
Plot.areaY(mtaData, Plot.windowY(Plot.normalizeY({
basis: 'max',
k: movingN,
x: "date",
y: "value",
fill: "type",
title: d => `${d.date} - ${d.value}`
}))),
],
color: {
legend: true,
scheme: 'dark2'
}
})
Insert cell
Plot.plot({
marginLeft: 100,
height: 600,
width: width,
facet: {
data: mtaData,
y: 'type',
marginRight: 150,
},
marks: [
Plot.ruleY([0]),
Plot.linearRegressionY(mtaData, {
x: "date",
y: "value",
stroke: "#000"
}),
Plot.lineY(mtaData, {
x: "date",
y: "value",
stroke: "type",
title: d => `${d.date} - ${d.value}`
}),
],
color: {
legend: true,
scheme: 'dark2'
}
})
Insert cell
Plot = tooltipPlugin(await require("@observablehq/plot"))
Insert cell
// This is a tooltip plugin that allows us to build more bespoke pop-overs.
import {tooltipPlugin} from "@mkfreeman/plot-tooltip"
Insert cell
Insert cell
Insert cell
import { db } from "@observablehq/google-merchandise-sales-data"
Insert cell
db
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
db
SELECT * FROM events
JOIN items ON events.item_id = items.id
WHERE type = 'purchase'
Insert cell
purchases
Insert cell
Plot.plot({
marks: [
Plot.dot(purchases, {x: "date", y: "price_in_usd"})
]
})
Insert cell
Plot.plot({
marks: [
Plot.rectY(
purchases,
Plot.binX({ y: "count" }, { x: "date", thresholds: d3.utcWeek, fill: "category" })
),
Plot.ruleY([0])
]
})
Insert cell
Plot.plot({
marks: [
Plot.rectY(
purchases,
Plot.binX({ y: "sum" }, { x: "date", y: "price_in_usd", thresholds: d3.utcWeek, fill: "category" })
),
Plot.ruleY([0])
],
color: { legend: true }
})
Insert cell
Plot.plot({
facet: {
data: purchases,
y: "category",
},
marks: [
Plot.rectY(
purchases,
Plot.binX({ y: "sum" }, { x: "date", y: "price_in_usd", thresholds: d3.utcWeek, fill: "category" })
),
Plot.ruleY([0])
],
color: { legend: true }
})
Insert cell
Plot.plot({
marks: [
Plot.rectY(
purchases,
Plot.binX(
{ y: "sum" },
{
x: "date",
y: "price_in_usd",
thresholds: d3.utcWeek,
fill: "category",
offset: "expand"
}
)
),
Plot.ruleY([0])
],
color: { legend: true }
})
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