Public
Edited
Feb 9, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// This cell is a Javascript cell. The results of the calculation is shown above the cell.
1 + 3
Insert cell
Insert cell
width
Insert cell
Insert cell
height = width * 0.3
Insert cell
Insert cell
Insert cell
myVar = 12
Insert cell
myVar += 10 // The SyntaxError in this cell is intentional
Insert cell
Insert cell
Insert cell
{
let myVar = 15;
return myVar;
}
Insert cell
gb.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
myVar
Insert cell
Insert cell
"height value is " + height
Insert cell
`height value is ${height}`
Insert cell
height
Insert cell
Insert cell
Insert cell
Insert cell
<svg width = ${width} height = ${height}>
<circle cx = "100" cy = "100" r = 5></circle>
</svg>
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@7")
Insert cell
mpg = await d3.csv("https://raw.githubusercontent.com/tidyverse/ggplot2/main/data-raw/mpg.csv");
Insert cell
Insert cell
Insert cell
Insert cell
viewof rows = Inputs.table(mpg)

Insert cell
Insert cell
Insert cell
gb.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
ukCities = FileAttachment("gb.csv").csv()

Insert cell
// TODO: add code to preview data in a table
viewof ukCity = Inputs.table(ukCities)
Insert cell
c = 40;



Insert cell
f = 1.800*c +32;
Insert cell
Insert cell
Insert cell
vl.markPoint()
.data(mpg)
.encode(
vl.x().field("displ").type("quantitative"),
vl.y().field("hwy").type("quantitative")
)
.render()
Insert cell
Insert cell
vl.markPoint()
.data(mpg)
.encode(
vl.x().field("displ").type("quantitative"),
vl.y().field("hwy").type("quantitative")
)
.render({renderer: "svg"}) // changed
Insert cell
Insert cell
Insert cell
vl.markPoint()
.data(mpg)
.encode(
vl.x().fieldQ("displ"), // changed
vl.y().fieldQ("hwy") // changed
)
.render()
Insert cell
Insert cell
vl.markPoint()
.data(mpg)
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy"),
vl.color().fieldN("drv") // changed
)
.render()
Insert cell
Insert cell
Insert cell
{
const plot = vl.markPoint()
.data(mpg)
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy"),
vl.color().fieldN("drv")
)
return plot.toObject()
}
Insert cell
Insert cell
{
const plot = vl.markPoint()
.data(mpg)
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy"),
vl.color().fieldN("drv")
)
return vl.vegalite.compile(plot.toObject()).spec // changed
}
Insert cell
Insert cell
Insert cell
{
const plot = vl.markPoint({filled: true}) // changed
.data(mpg)
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy"),
vl.color().fieldN("drv")
)
return plot.render()
}
Insert cell
Insert cell
// TODO make a scatter plot here.
{
const plot = vl.markPoint({filled: true}) // changed
.data(ukCities)
.encode(
vl.x().fieldQ("population").scale({type:"log"}),
vl.y().fieldQ("population_proper").scale({type:"log"}),
)
return plot.render()
}
Insert cell
Insert cell
vl.markLine() // changed mark type
.data(mpg)
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy"),
vl.stroke().fieldN("drv") // encode stroke color (instead of previously fill color)
)
.transform( // added the transformation
vl.regression("hwy").on("displ").groupby("drv")
)
.render()
Insert cell
Insert cell
Insert cell
Insert cell
{
const pointPlot = vl.markPoint()
.data(mpg)
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy"),
vl.color().fieldN("drv")
)

// ------------------------
const linePlot = vl.markLine()
.data(mpg)
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy"),
vl.stroke().fieldN("drv")
)
.transform(
vl.regression("hwy").on("displ").groupby("drv")
)

// ------------------------
// layering
return vl.layer(pointPlot, linePlot)
.render()
}
Insert cell
Insert cell
{
const pointPlot = vl.markPoint()
.encode(
vl.color().fieldN("drv")
)

const linePlot = vl.markLine()
.encode(
vl.stroke().fieldN("drv")
)
.transform(
vl.regression("hwy").on("displ").groupby("drv")
)

return vl.layer(pointPlot, linePlot)
.data(mpg) // common specification
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy")
)
.render()
}
Insert cell
Insert cell
Insert cell
vl.markBar()
.data(mpg)
.encode(
vl.y().fieldQ("hwy").bin(true),
vl.x().aggregate("count")
)
.render()
Insert cell
Insert cell
{
const pointPlot = vl.markPoint()
.encode(
vl.color().fieldN("drv")
);

const linePlot = vl.markLine()
.encode(
vl.stroke().fieldN("drv")
)
.transform(
vl.regression("hwy").on("displ").groupby("drv")
);

const mainPlot = vl.layer(pointPlot, linePlot)
.data(mpg)
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy")
);

//-----------------------
const histogramPlot = vl.markBar()
.data(mpg)
.encode(
vl.y().fieldQ("hwy").bin(true).scale({domain: [0, 45]}), // same scale for both plots
vl.x().aggregate("count").scale({domain: [0, 100]}) // also fix the scale here
)
.width(100); // added specification for the plot width

//-----------------------
// concatenation
return vl.hconcat(histogramPlot, mainPlot)
.render()
}
Insert cell
Insert cell
Insert cell
{
const pointPlot = vl.markPoint()
.encode(
vl.color().fieldN("drv")
);

const linePlot = vl.markLine()
.encode(
vl.stroke().fieldN("drv")
)
.transform(
vl.regression("hwy").on("displ").groupby("drv")
);

const mainPlot = vl.layer(pointPlot, linePlot)
.data(mpg)
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy")
);

//-----------------------
const histogramPlot = vl.markBar()
.data(mpg)
.encode(
vl.y().fieldQ("hwy").bin(true).scale({domain: [0, 45]}), // same scale for both plots
vl.x().aggregate("count").scale({domain: [0, 100]}) // also fix the scale here
)
.width(100); // added specification for the plot width

//-----------------------
// concatenation
return vl.hconcat(vl.hconcat(histogramPlot, mainPlot ), histogramPlot )
.render()
}
Insert cell
Insert cell
Insert cell
{

// creating a parameter
const xMaxParam = vl.param("x_max")
.value(10) // initial value
.bind(vl.slider(0, 10, 1).name("Max. engine displacement: ")) // adding slider with min, max, step

const plot = vl.markPoint()
.data(mpg)
.encode(
vl.x().fieldQ("displ").scale({domain: [0, 10]}), // fix the horizontal axis scale
vl.y().fieldQ("hwy").scale({domain: [0, 45]}), // ditto for vertical axis
vl.color().fieldN("drv")
)
.params(xMaxParam) // specify that we use this parameter in the spec
.transform(
vl.filter("datum.displ < x_max") // filter to check each data point (referred to as `datum`)
)

return plot.render()
}
Insert cell
Insert cell
{
const xMaxParam = vl.param("x_max")
.value(10)
.bind(vl.slider(0, 10, 1).name("Max. engine displacement: "))

const pointPlot = vl.markPoint()
.encode(
vl.color().fieldN("drv")
)
const linePlot = vl.markLine()
.encode(
vl.stroke().fieldN("drv")
)
.transform(
vl.regression("hwy").on("displ").groupby("drv")
)

// layering
return vl.layer(pointPlot, linePlot)
.data(mpg)
.encode(
vl.x().fieldQ("displ").scale({domain: [0, 10]}),
vl.y().fieldQ("hwy").scale({domain: [0, 45]})
)
.params(xMaxParam) // param
.transform(
vl.filter("datum.displ < x_max")
)
.render()
}
Insert cell
Insert cell
{
const selectedPoint = vl.selectPoint("selected_point") // creating the point selection
.on("click") // mouse click for selection
.nearest(true) // when clicked, select the nearest point
.clear("dblclick"); // double click to clear selection
const plot = vl.markPoint()
.data(mpg)
.params(selectedPoint) // param
.encode(
vl.x().fieldQ("displ").scale({domain: [0, 10]}),
vl.y().fieldQ("hwy").scale({domain: [0, 45]}),
vl.color().fieldN("drv"),
vl.opacity().if(selectedPoint, vl.value(1)).value(0.3), // change opacity based on selection
vl.size().if(selectedPoint, vl.value(100)).value(10) // change opacity based on selection
)
return plot.render()
}
Insert cell
Insert cell
{
const selectedInterval = vl.selectInterval("selected_interval"); // creating the interval selection
const plot = vl.markPoint()
.data(mpg)
.params(selectedInterval)
.encode(
vl.x().fieldQ("displ").scale({domain: [0, 10]}),
vl.y().fieldQ("hwy").scale({domain: [0, 45]}),
vl.color().fieldN("drv"),
vl.opacity().if(selectedInterval, vl.value(1)).value(0.3),
vl.size().if(selectedInterval, vl.value(100)).value(10)
)
return plot.render()
}
Insert cell
Insert cell
{
const selectedInterval = vl.selectInterval("selected_interval")
.encodings("y"); // restricting to vertical selection (y-axis)
const plot = vl.markPoint()
.data(mpg)
.params(selectedInterval)
.encode(
vl.x().fieldQ("displ").scale({domain: [0, 10]}),
vl.y().fieldQ("hwy").scale({domain: [0, 45]}),
vl.color().fieldN("drv"),
vl.opacity().if(selectedInterval, vl.value(1)).value(0.3),
vl.size().if(selectedInterval, vl.value(100)).value(10)
)
return plot.render()
}
Insert cell
Insert cell
{
const selectedInterval = vl.selectInterval("selected_interval")
.encodings("y");
const pointPlot = vl.markPoint()
.params(selectedInterval)
.encode(
vl.color().fieldN("drv"),
vl.opacity().if(selectedInterval, vl.value(1)).value(0.3),
vl.size().if(selectedInterval, vl.value(100)).value(10)
);

const linePlot = vl.markLine()
.encode(
vl.stroke().fieldN("drv")
)
.transform(
vl.filter(selectedInterval), // add selection interval
vl.regression("hwy").on("displ").groupby("drv")
);
const mainPlot = vl.layer(pointPlot, linePlot)
.data(mpg)
.encode(
vl.x().fieldQ("displ"),
vl.y().fieldQ("hwy")
);

//-----------------------
const histogramPlot = vl.markBar()
.data(mpg)
.transform(
vl.filter(selectedInterval), // add selection interval
)
.encode(
vl.y().fieldQ("hwy").bin(true).scale({domain: [0, 45]}),
vl.x().aggregate("count").scale({domain: [0, 100]})
)
.width(100);

//-----------------------
// concatenation
return vl.hconcat(histogramPlot, mainPlot)
.render()
}
Insert cell
Insert cell
// TODO: Write your code here
Insert cell
Insert cell
Insert cell
ukCities // This cell should not have RuntimeError if you have loaded the data from exercise 1 above
Insert cell
Insert cell
vl.markSquare({size: 1, opacity: 1})
.data(ukCities)
.encode(
vl.longitude().fieldQ('lng'),
vl.latitude().fieldQ('lat')
)
.config({view: {stroke: null}})
.render();
Insert cell
Insert cell
ukTopo = await d3.json("https://raw.githubusercontent.com/deldersveld/topojson/master/countries/united-kingdom/uk-counties.json");
Insert cell
{
const map = vl.markGeoshape({fill: '#ddd', stroke: '#fff', strokeWidth: 1})
.data(vl.topojson(ukTopo).feature('GBR_adm2'));
return map.render()
}
Insert cell
Insert cell
// TODO: write your code here
Insert cell
Insert cell
Insert cell
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