I’ve been (mostly) keeping up with annual updates for my R/{sf} U.S. foliage post which you can find on GH. This year, we have Quarto, and it comes with so many batteries included that you’d think it was Christmas. One of those batteries is full support for the Observable runtime. These are used in {ojs}
Quarto blocks, and rendered versions can run anywhere.
The Observable platform is great for both tinkering and publishing (we’re using it at work for some quick or experimental vis work), and with a few of the recent posts, here, showing how to turn Observable notebooks into Quarto documents, you’re literally two clicks or one command line away from using any public Observable notebook right in Quarto.
I made a version of the foliage vis in Observable and then did the qmd conversion using the Chrome extension, tweaked the source a bit and published the same in Quarto.
The interactive datavis uses some foundational Observable/D3 libraries:
- d3/choropleth: for making the choropleth map
- mbostock/scrubber: for making the slider/play control
- d3/color-legend: for displaying a color swatch legend
In the JS code we set some datavis-centric values:
foliage_levels = [0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
foliage_colors = ["#83A87C", "#FCF6B4", "#FDCC5C", "#F68C3F", "#EF3C23", "#BD1F29", "#98371F"]
foliage_labels = ["No Change", "Minimal", "Patchy", "Partial", "Near Peak", "Peak", "Past Peak"]
week_label = ["Sept 5th", "Sept 12th", "Sept 19th", "Sept 26th", "Oct 3rd", "Oct 10th", "Oct 17th", "Oct 24th", "Oct 31st", "Nov 7th", "Nov 14th", "Nov 21st"]
We then borrow the U.S. Albers-projected topojson file from the Choropleth
example notebook and rebuild the outline mesh and county geometry collections, since we need to get rid of Alaska and Hawaii (they’re not present in the source data). We do this by filtering out two FIPS codes:
counties = {
var cty = topojson.feature(us, us.objects.counties);
cty.features = cty.features.filter(
(d) => (d.id.substr(0, 2) != "02") & (d.id.substr(0, 2) != "15")
);
return cty;
}
I also ended up modifying the source CSV a bit to account for missing counties.
After that, it was a straightforward call to our imported Choropleth
function:
chart = Choropleth(rendered2022, {
id: (d) => d.id.toString().padStart(5, "0"), // this is needed since the CSV id column is numeric
value: (d) => d[week_label.indexOf(week) + 1], // this gets the foliage value based on which index the selected week is at
scale: d3.scaleLinear, // this says to map foliage_levels to foliage_colors directly
domain: foliage_levels,
range: foliage_colors,
title: (f, d) =>
`${f.properties.name}, ${statemap.get(f.id.slice(0, 2)).properties.name}`, // this makes the county hover text the county + state names
features: counties, // this is the counties we modified
borders: statemesh, // this is the statemesh
width: 975,
height: 610
})
and placing the legend and scrubbing slider.
The only real difference between the notebook and qmd is the inclusion of the source functions rather than use Observable’s import
(I’ve found that there’s a slight load delay for import
s when network conditions aren’t super perfect and the inclusion of the source — WITH copyrights — makes up for that).
I’ve set up the Quarto project so that renders go to the docs/
directory, which makes it easy to publish as a GH page.
FIN
Drop issues on GH if anything needs clarifying or fixing and go experiment! You can’t break anything either on Observable or locally that version control can’t fix (yes, Observable has version control!).
Some things to consider modifying/adding:
- have a click take you to a (selectable?) mapping service, so folks can get driving directions
- turn the hover text into a proper tooltip
- speed up or slow down the animation when ‘Play’ is tapped
- use different colors
- bring in older datasets (see the
foliage
GH repo) and make multiple maps or let the user select them or have them compare across years