Published
Edited
Feb 10, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
covid = d3.csvParse(await FileAttachment("VDH-COVID-19-PublicUseDataset-Cases.csv").text(), function(d) {
return {
date: d3.timeParse("%x")(d["Report Date"]),
fips: d.FIPS,
locality: d.Locality,
district: d["VDH Health District"],
cases: +d["Total Cases"],
hospitalizations: +d.Hospitalizations,
deaths: +d.Deaths
};
});
Insert cell
Insert cell
covid.sort(function (a, b) {
return a.date - b.date;
});
Insert cell
covid.sort((a, b) => a.date - b.date)
Insert cell
Insert cell
printTable(covid.slice(0,7))
Insert cell
printTable(covid.slice(-7))
Insert cell
Insert cell
hr_districts = new Set(["Western Tidewater", "Chesapeake", "Virginia Beach", "Norfolk", "Portsmouth", "Hampton", "Peninsula"])
Insert cell
hr_covid = covid.filter(d => hr_districts.has(d.district))
Insert cell
Insert cell
Insert cell
printTable(hr_covid.slice(0,10))
Insert cell
printTable(hr_covid.slice(-10))
Insert cell
last_date = covid[covid.length-1].date
Insert cell
covid_totals = covid.filter(d => d.date >= last_date)
Insert cell
printTable(covid_totals.slice(0,10))
Insert cell
hr_totals = covid_totals.filter(d => hr_districts.has(d.district))
Insert cell
printTable(hr_totals)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof scatter = vl.markPoint()
.data(covid_totals)
.encode(
vl.x().fieldQ("cases"),
vl.y().fieldQ("deaths"),
vl.color().fieldN("locality"),
vl.tooltip(["locality","cases","deaths"])
)
.title(
{text:"Total Cases Vs. Deaths per locality"}
)
.render()
Insert cell
Insert cell
Insert cell
scatterplot2 = d3.rollups(covid, v => [d3.sum(v, d => d.cases),d3.sum(v, d => d.hospitalizations)], d => d.date)
Insert cell
md`*This code calculates the total cases and total hospitalizations in VA over time. It gets the total cases and total hopitalizations per each day.*`
Insert cell
scatterplot2_json = scatterplot2.map(obj => { let rObj= {}; rObj["date"] = obj[0]; rObj["totals"] = obj[1]; return rObj;})
Insert cell
viewof scatter2 = vl.markPoint({tooltip: {"content": "data"}})
.data(covid)
.encode(
vl.x().sum("cases"),
vl.y().sum("hospitalizations"),
vl.color().fieldT("date")
)
.title({text:"Total Cases vs. Hospitalizations in VA over time"})
.render()
Insert cell
Insert cell
Insert cell
fields =["cases", "hospitalizations", "deaths"]
Insert cell
viewof line = vl.markLine({tooltip: {"content": "data"}})
.data(covid)
.encode(
vl.x().fieldT("date"),
vl.y().sum(vl.repeat("column"))
)
.width(200).height(200)
.repeat({
column: ["cases", "hospitalizations", "deaths"]
})
.title({text:"Time series of Total Cases, Hospitalizations, and Deaths in VA over time"})
.render()
Insert cell
Insert cell
Insert cell
viewof layeredline = vl.markLine({tooltip: {"content": "data"}})
.data(hr_covid)
.encode(
vl.x().fieldT("date"),
vl.y().sum("cases"),
vl.color().fieldN("district")
)
.title({text:"Time series of Total Cases over time for Hampton Roads Health Districts "})
.render()
Insert cell
Insert cell
Insert cell
viewof facetedLineChart = vl.markLine({tooltip: {"content": "data"}})
.data(hr_covid)
.encode(
vl.column().fieldN("district"),
vl.x().fieldT("date"),
vl.y().sum("cases")
)
.width(100).height(100)
.title({text:"Time series of Total Cases over time for Hampton Roads Health Districts "})
.render()
Insert cell
Insert cell
Insert cell
viewof layeredBar = vl
.data(hr_totals)
.layer(
vl.markBar({tooltip: {"content": "data"}})
.encode(
vl.x().sum("cases"),
vl.y().fieldN("district").sort(vl.sum("cases").order("descending")),
vl.color().fieldN("district"),
)
)
.title({text:"Total Cases for Hampton Roads Health Districts"})
.render()
Insert cell
Insert cell
Insert cell
district_totals = d3.rollups(covid_totals, v => d3.sum(v, d => d.cases), d => d.district)
Insert cell
Insert cell
district_covid_totals = district_totals.map(
function(value){
return { "district": value[0], "total_cases": value[1]}
})
Insert cell
Insert cell
viewof boxplot2 = vl.markBoxplot({tooltip: {"content": "data"}})
.data(district_covid_totals)
.encode(
vl.x().fieldQ("total_cases")
)
.width("600").height("100")
.title({text:"Total Cases per Health District"})
.render()
Insert cell
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
import { vl } from "@vega/vega-lite-api"
Insert cell
import {printTable} from '@uwdata/data-utilities'
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