Published
Edited
Jun 15, 2020
Insert cell
Insert cell
import {vl} from '@vega/vega-lite-api'
Insert cell
vl.markBar()
.data('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv')
.transform(
vl.filter('month(datum.date) >= 2')
)
.encode(
vl.y().field('cases').type('quantitative').title('Cases'),
vl.x().field('date').type('temporal').title('Date')
)
.title('Cumulative U.S. Covid Cases')
.render()
Insert cell
barChart = vl.markBar()
.data('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv')
.transform(
vl.filter('month(datum.date) >= 2'),
vl.window(
vl.first_value('cases').as('previous_cases')
).frame(-1, 0),
vl.calculate('datum.cases - datum.previous_cases').as('new_cases')
)
.encode(
vl.y().field('new_cases').type('quantitative').title('Cases'),
vl.x().field('date').type('temporal').title('Date')
)
.title('Daily New U.S. Covid Cases')
.height(400)
.width(width)
Insert cell
lineChart = vl.markLine({color: "red"})
.data('https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv')
.transform(
vl.filter('month(datum.date) >= 2'),
vl.window(
vl.first_value('cases').as('previous_cases')
).frame(-1, 0),
vl.calculate('datum.cases - datum.previous_cases').as('new_cases'),
vl.window(vl.mean('new_cases').as('rolling_mean')).frame(-6, 0)
)
.encode(
vl.y().field('rolling_mean').type('quantitative').title('Cases'),
vl.x().field('date').type('temporal').title('Date')
)
.title('Daily 7-Day Average of new U.S. Covid Cases')
.height(400)
.width(width)
Insert cell
barChart.render()
Insert cell
lineChart.render()
Insert cell
chart = vl.layer(barChart, lineChart).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