Published
Edited
Mar 29, 2022
1 fork
22 stars
Insert cell
Insert cell
Insert cell
data = (await require('vega-datasets@2'))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
usemployment = data['us-employment.csv']()
Insert cell
Plot.barY(usemployment, {x:'month', y:'nonfarm_change'}).plot()
Insert cell
Insert cell
Plot.barY(usemployment, {x:'month', y:'nonfarm_change'}).plot({
x: {
ticks: d3.utcYear.range(...d3.extent(usemployment, (d) => d.month)),
tickFormat: "%Y"
}
})
Insert cell
Insert cell
Plot.areaY(usemployment,
{x:'month', y:'nonfarm_change', curve:'step'}).plot()
Insert cell
Insert cell
Plot.rectY(usemployment,
Plot.binX({ y: "sum" },
{ x: "month", y: "nonfarm_change", thresholds: d3.utcMonth, inset: 0 })
).plot()
Insert cell
Insert cell
Plot.plot({
color: {
domain: ["positive", "negative"],
range: ["steelblue", "orange"]
},
marks: [
Plot.areaY(usemployment, {
x:'month',
y:'nonfarm_change',
curve:'step',
fill: d => d.nonfarm_change > 0 ? "positive" : "negative"})
]
})
Insert cell
Insert cell
Plot.plot({
color: {
domain: ["positive", "negative"],
range: ["steelblue", "orange"]
},
marks: [
Plot.rectY(usemployment, Plot.binX({ y: "sum" }, { x: "month", y: "nonfarm_change", thresholds: d3.utcMonth, inset: 0, fill: d => d.nonfarm_change > 0 ? "positive" : "negative"}))
]
})
Insert cell
Insert cell
Plot.plot({
color: {
domain: ["positive", "negative"],
range: ["steelblue", "orange"]
},
marks: [
Plot.barY(usemployment, {
x:'month',
y:'nonfarm_change',
fill: d => d.nonfarm_change > 0 ? "positive" : "negative"
}),
Plot.ruleY([0])
],
x: {
ticks: d3.utcYear.range(...d3.extent(usemployment, (d) => d.month)),
tickFormat: "%Y"
}
})
Insert cell
Insert cell
Plot.plot({
color: {
domain: ["positive", "negative"],
range: ["steelblue", "orange"]
},
marks: [
Plot.barY(usemployment, {
x:'month',
y:'nonfarm_change',
fill: d => d.nonfarm_change > 0 ? "positive" : "negative",
stroke: d => d.nonfarm_change > 0 ? "positive" : "negative"
}),
Plot.ruleY([0])
],
x: {
ticks: d3.utcYear.range(...d3.extent(usemployment, (d) => d.month)),
tickFormat: "%Y"
}
})
Insert cell
Insert cell
Insert cell
barley = data["barley.json"]()
Insert cell
Plot.plot({
facet: {
data: barley,
x: "site"
},
marks : [
Plot.barY(barley, Plot.groupX({y: "mean"}, {x: "year", y: "yield", fill:"year"}))
]
})
Insert cell
Insert cell
barley_converted = (await data["barley.json"]()).map(d=>({
year: d['year'].toString(),
yield: d['yield'],
site: d['site']
}))
Insert cell
Plot.plot({
facet: {
data: barley_converted,
x: "site"
},
color:{legend:true},
marks : [
Plot.barY(barley_converted, Plot.groupX({y: "mean"}, {x: "year", y: "yield", fill:"year"}))
]
})
Insert cell
Insert cell
iowa = data['iowa-electricity.csv']()
Insert cell
Plot.plot({
y: {
grid: true
},
color:{legend:true},
marks: [
Plot.barY(iowa, {x: "year", y2: "net_generation", fill: "source", mixBlendMode: "normal", fillOpacity:0.7}),
Plot.ruleY([0])
]
})
Insert cell
Plot.plot({
x: {
ticks: d3.timeTicks(...d3.extent(iowa, (d) => d.year), d3.utcYear),
tickFormat: "%Y"
},
y: {
grid: true
},
color:{legend:true},
marks: [
Plot.barY(iowa, {x: "year", y2: "net_generation", fill: "source", mixBlendMode: "normal", fillOpacity:0.7}),
Plot.ruleY([0])
]
})

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
marginLeft: 110,
facet:{
data: barley,
x: "year",
},
color:{legend:true},
marks: [
Plot.barX(barley, {x:"yield", y:"variety", fill:"site", order:"site", reverse:'true'})
]
})
Insert cell
Insert cell
Insert cell
stocks = (await data['stocks.csv']()).map(d=>({
date: new Date(d['date']),
price: +d.price,
symbol: d.symbol
}))
Insert cell
Plot.plot({
color:{legend:true},
marks:[
Plot.line(stocks, {x:'date', y:'price', stroke:'symbol'})
]
})
Insert cell
Insert cell
google = stocks.filter(d=>d.symbol==='GOOG')
Insert cell
Plot.plot({
marks:[
Plot.line(google, {x:'date', y:'price', curve:'step', stroke:'steelblue', strokeWidth:2}),
Plot.areaY(google, {x:'date', y:'price', curve:'step', fill:'steelblue', fillOpacity:0.3}),
]
})
Insert cell
Insert cell
Plot.plot({
width:180,
y:{inset:10},
color:{legend:true},
marks : [
Plot.lineY(barley_converted, Plot.groupX({y: "median"}, {x: "year", y: "yield", stroke:"site"})),
Plot.dotY(barley_converted, Plot.groupX({y: "median"}, {x: "year", y: "yield", fill:"site"}))
]
})
Insert cell
Insert cell
Insert cell
welders = (await data['jobs.json']()).filter(d=>d.job==='Welder').map(d=>({
date: new Date(d.year+'-01-01'), perc:+d.perc, sex:d.sex
}))
Insert cell
Plot.plot({
inset:20,
y:{
tickFormat: d3.format(".1%"),
grid:true
},
color:{legend:true},
marks:[
Plot.ruleY([0]),
Plot.line(welders, {x:"date", y:"perc", stroke:'sex', strokeWidth:3})
]
})
Insert cell
Insert cell
Insert cell
Plot.plot({
color:{legend:true},
marks:[
Plot.areaY(iowa, Plot.stackY({
x:'year',
y:'net_generation',
z:'source',
fill:'source',
offset: "expand",
reverse:true
}))
]
})
Insert cell
Insert cell
Plot.plot({
marginLeft:50,
color:{legend:true},
marks: [
Plot.areaY(iowa, {
x: "year", y2: "net_generation",
fill: "source", mixBlendMode: "normal", fillOpacity:0.3}),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
Plot.plot({
grid:true,
facet:{
data:iowa,
y:'source',
marginRight:100,
marginLeft:20
},
color:{legend:true},
marks: [
Plot.areaY(iowa, {
x: "year", y2: "net_generation",
fill: "source"}),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
unemployment = (await data['unemployment-across-industries.json']()).map(d=>({
date: new Date(d.date),
count: d.count,
series: d.series
}))
Insert cell
Plot.plot({
x: {
grid: true,
},
color:{legend:true},
marks: [
Plot.areaY(unemployment, Plot.stackY({offset: "silhouette", x: "date", y: "count", fill: "series"}))
]
})
Insert cell
Insert cell
Insert cell
cars = data['cars.json']()
Insert cell
Plot.plot({
inset:20,
color:{legend:true},
marks:[
Plot.dot(cars, {
x:'Horsepower',
y:'Miles_per_Gallon',
fill:'Origin',
r:10, stroke:'black', strokeWidth:1
})
]
})

Insert cell
Insert cell
commits = (await data['github.csv']()).map(d=>({
time: new Date(d.time), count:+d.count
}))
Insert cell
Plot.plot({
x: {
inset: 10,
ticks: 24
},
y: {
type: "point",
tickFormat: Plot.formatWeekday()
},
marks: [
Plot.dot(commits, Plot.group({r: "sum", title: "sum"}, {
y: d => d.time.getDay(),
x: d => d.time.getHours(),
r: "count",
fill: "steelblue"
}))
]
})
Insert cell
Insert cell
seattle_weather = data['seattle-weather.csv']()
Insert cell
Plot.plot({
grid:true,
marks:[
Plot.dot(seattle_weather, {x: 'date', y:'temp_max', stroke:'steelblue'}),
Plot.line(seattle_weather, Plot.windowY({x: "date", y: "temp_max", k: 15, stroke: "red", strokeWidth:3}))
]
})
Insert cell
Insert cell
Insert cell
movies = (await data['movies.json']()).map(d=>({
'Major Genre':String(d['Major Genre']).split('/')[0],
'IMDB Rating':d['IMDB Rating']
}))
Insert cell
Plot.plot({
width:width,
x:{
axis:false
},
facet:{
data:movies,
x: 'Major Genre',
},
marks:[
Plot.dot(movies, {
y:'IMDB Rating',
x:d=>Math.sqrt(-2*Math.log(Math.random()))*Math.cos(2*Math.PI*Math.random()),
fill:'Major Genre'})
]
})
Insert cell
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