Public
Edited
Oct 29, 2021
1 fork
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
aq = require("arquero")
Insert cell
// Load the data
rawData = aq.loadCSV(dataUrl, {header: false, names: names })
Insert cell
Insert cell
Insert cell
Inputs.table(rawData)
Insert cell
// As an array of objects
data = rawData.objects()
Insert cell
Insert cell
Insert cell
Q1: What is the range of altitude of airports in India?

- Step 1: Filter for India
- Step 2: Summarize Max and Min of Altitude
- Step 3: Calculate / Derive the Difference of Max & Min

Filter
- Take the Data
- Apply a condition (function)
Insert cell
// India Filter Function
function indiaFilter(d) {
return d["country"] === "India";
}
Insert cell
// Short Arrow function - Think of d as a row
indiaFilterShort = (d) => d.country === "India"
Insert cell
Insert cell
indiaData = rawData.filter(indiaFilter)
Insert cell
indiaDataShort = rawData.filter(indiaFilterShort)
Insert cell
indiaDataFull = rawData.filter((d) => d["country"] === "India")
Insert cell
Insert cell
import { Wrangler, op } from "@observablehq/data-wrangler"
Insert cell
Wrangler(data)
Insert cell
// To use copied code replace "data" with your own variable
indiaDataWrangler = aq
.from(data)
.filter((d) => d["country"] === "India")
.select("iata", "altitude", "city")
.orderby(aq.desc("altitude"))
Insert cell
Inputs.table(indiaDataWrangler)
Insert cell
vegaEmbed = require("vega-embed")
Insert cell
vegaEmbed({
width: width,
data: { values: indiaDataWrangler },
mark: "point",
encoding: {
x: { field: "iata", type: "nominal", sort: "-y" },
y: { field: "altitude", type: "quantitative" }
}
})
Insert cell
Insert cell
vegaEmbed({
width: width,
data: { values: indiaDataWrangler },
mark: "bar",
encoding: {
x: { field: "iata", type: "nominal", sort: "-y" },
y: { field: "altitude", type: "quantitative" }
}
})
Insert cell
vegaEmbed({
width: width,
data: { values: indiaDataWrangler },
mark: "point",
encoding: {
x: { field: "altitude", type: "quantitative" },
tooltip: { field: "city" }
}
})
Insert cell
vegaEmbed({
width: 400,
heigth: 200,
data: { values: indiaDataWrangler },
mark: "tick",
encoding: {
x: { field: "iata", type: "nominal", axis: null },
color: {
field: "altitude",
type: "quantitative",
axis: null,
scale: { scheme: "spectral" }
},
size: { value: 100 }
},
config: { axis: { grid: false } }
})
Insert cell
vegaEmbed({
width: 400,
heigth: 100,
data: { values: indiaDataWrangler },
mark: "tick",
encoding: {
x: { field: "iata", type: "nominal", axis: null },
color: {
field: "altitude",
type: "quantitative",
axis: null,
scale: {
range: [
"#ffd700",
"#ffb14e",
"#fa8775",
"#ea5f94",
"#cd34b5",
"#9d02d7",
"#0000ff"
]
}
},
size: { value: 100 }
},
config: { axis: { grid: false } }
})
Insert cell
Insert cell
Inputs.table(binnedAltitude)
Insert cell
binnedAltitude = aq
.from(data)
.filter((d) => d["country"] === "India")
.select("iata", "altitude")
.groupby({
bin_start: aq.bin("altitude", { step: 200 }),
bin_end: aq.bin("altitude", { step: 200, offset: 1 })
})
.count()
Insert cell
vegaEmbed({
width: 500,
data: { values: binnedAltitude },
mark: "bar",
encoding: {
x: { field: "bin_start", type: "quantitative" },
x2: { field: "bin_end", type: "quantitative" },
y: { field: "count", type: "quantitative" }
}
})
Insert cell
vegaEmbed({
data: { values: indiaDataWrangler },
mark: "bar",
encoding: {
x: { field: "altitude", type: "quantitative", bin: true },
y: { aggregate: "count", type: "quantitative" }
}
})
Insert cell
Insert cell
table = aq
.from(data)
.select("country")
.groupby("country")
.count()
.orderby(aq.desc("count"))
Insert cell
Inputs.table(table)
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