Published
Edited
Feb 28, 2021
Insert cell
Insert cell
Insert cell
Insert cell
airbnb = aq.fromCSV( await FileAttachment("airbnb@7.csv").text() )
Insert cell
neighbourhoods = airbnb.rollup({ nb: d => op.unique(d.neighbourhood) }).objects()[0]["nb"]
Insert cell
airbnb.view()
Insert cell
md`## Part (a)`
Insert cell
{
const map = vl.markCircle({size: 2})
.data(airbnb)
.encode(
vl.x().fieldQ('longitude').scale({zero: false}),
vl.y().fieldQ('latitude').scale({zero: false}),
vl.color().fieldN('room_type').scale({scheme: "set2"})
)
.height(400);

return map.render();
}
Insert cell
{
// dynamic query histogram
const prices = vl.markBar()
.encode(
vl.x().fieldQ('log_price'),
vl.y().count().title(null),
vl.color().fieldN('room_type').scale({scheme: "set2"})
)
.height(100);
// map scatter plot
const map = vl.markCircle({size: 2})
.encode(
vl.x().fieldQ('longitude').scale({zero: false}),
vl.y().fieldQ('latitude').scale({zero: false}),
vl.color().fieldN('room_type').scale({scheme: "set2"})
)
.height(400);

return vl.data(airbnb)
.vconcat(prices, map)
.render();
}
Insert cell
{
const brush = vl.selectInterval()
.encodings('x'); // limit selection to x-axis (year) values

// dynamic query histogram
const years = vl.markBar()
.select(brush)
.encode(
vl.x().fieldQ('log_price'),
vl.y().count().title(null),
vl.color().fieldN('room_type').scale({scheme: "set2"})
)
.height(100);
// ratings scatter plot
const ratings = vl.markCircle()
.encode(
vl.x().fieldQ('longitude').scale({zero: false}),
vl.y().fieldQ('latitude').scale({zero: false}),
vl.color().fieldN('room_type').scale({scheme: "set2"}),
vl.opacity().if(brush, vl.value(0.8)).value(0.2),
vl.size().if(brush, vl.value(10)).value(0.5)
)
.height(400);

return vl.data(airbnb)
.vconcat(years, ratings)
.render();
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more