Unlisted
Edited
Apr 12
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cars.view(10)
Insert cell
Insert cell
Insert cell
scatterplot1 = FileAttachment("scatterplot@1.png").image({ width: 300 })
Insert cell
Insert cell
// Our code goes here: JSON version.
{
const chartSpec = {
data: {
values: cars.objects()
},
mark: 'circle',
encoding: {
x: {
field: 'Miles_per_Gallon',
type: 'quantitative'
},
y: {
field: 'Horsepower',
type: 'quantitative'
}
}
}

return vl.spec(chartSpec).render()
}
Insert cell
Insert cell
// Our code goes here: JavaScript version.
vl.mark({ type: 'circle' })
.encode(
vl.x().fieldQ('Miles_per_Gallon'),
vl.y().fieldQ('Horsepower')
)
.data(cars)
.render()
Insert cell
Insert cell
// Code: Plot with responsive dimensions
vl.mark({ type: 'circle' })
.data(cars)
.encode(
vl.x().fieldQ('Miles_per_Gallon'),
vl.y().fieldQ('Horsepower')
)
.width(0.3 * width)
.height(0.3 * width)
.render()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Code: plot with Origin
vl.mark({ type: 'circle' })
.data(cars)
.encode(
vl.x().fieldQ('Miles_per_Gallon')
.axis({ title: 'Miles per Gallon (mpg)' }),
vl.y().fieldQ('Horsepower'),
vl.color().fieldN('Origin'),
)
.width(0.3 * width)
.height(0.3 * width)
.render()
Insert cell
Insert cell
Insert cell
// Code: can we separate origin further? We've already "used up" the x and y coordinates.
vl.mark({ type: 'circle' })
.data(cars)
.encode(
vl.x().fieldQ('Miles_per_Gallon')
.axis({ title: 'Miles per Gallon (mpg)' }),
vl.y().fieldQ('Horsepower'),
vl.color().fieldN('Origin'),
vl.facet({ columns: 3 }).fieldN('Origin')
)
.width(0.25 * width)
.height(0.25 * width)
.render()
Insert cell
Insert cell
cars.sample(5).view()
Insert cell
cars.groupby('Cylinders').count().view()
Insert cell
// Number of cars of different cylinders
vl.markBar()
.data(cars)
.encode(
vl.x().fieldO('Cylinders'),
vl.y().count()
)
.render()
Insert cell
// Average acceleration of cars with different numbers of cylinders
vl.markBar()
.data(cars)
.encode(
vl.x().fieldO('Cylinders'),
vl.y().mean('Acceleration')
)
.render()
Insert cell
// Turning quantitative fields into Ordinal fields through binning
// What are the data?
// What are the channels?
// What's the mark?
vl.markBar()
.data(cars)
.encode(
vl.x().fieldQ('Miles_per_Gallon').bin(true),
vl.y().fieldQ('Horsepower').bin(true),
vl.color().count()
)
.render()
Insert cell
Insert cell
Insert cell
surveydata
.sample(5)
.view()
Insert cell
// People's birthdays
// What should do with the y-axis?
Insert cell
// Other time units
Insert cell
Insert cell
// Where do we hail from?
Insert cell
Insert cell
Insert cell
datasets = (await import('https://cdn.jsdelivr.net/npm/vega-datasets@3.1.0/+esm')).default;
Insert cell
cars = aq.from(await datasets['cars.json']())
Insert cell
Insert cell
Insert cell
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