temperatures = FileAttachment("temperatures.csv").csv({typed: true})
Plot.line(temperatures, {x: "Date", y: "Anomaly"}).plot({y: {grid: true}})
Using Observable in Quarto
Let’s look at how to embed Observable for easy interactive data visualization.
What is Observable?
Observable is blah blah.
Embedding Observable
Observable is embedded with the ojs
tag, so your .qmd
file might look something like this:
We are reading in a file.
```{ojs}
= FileAttachment("temperatures.csv").csv({typed: true})
temps ```
And here is our plot.
```{ojs}
.lineY(temps, {x: "Date", y: "Anomaly"})
Plot```
This could result in a chart like below:
Creating interactive plots
Note
This is an adaptation of the moving average example from the Observable documentation.
We’ll start by reading in our file. The file itself lives in the same folder as our .qmd
file.
= FileAttachment("temperatures.csv").csv({typed: true}) temps
The beauty of Observable is that it’s easy to create graphics that the user can interact with by setting options, filtering, and more! In this case we’ll use Inputs.select
to set the rolling average window for a time series plot.
= Inputs.select([6, 12, 24, 36, 60, 120], {
viewof rolling value: 12,
format: o => `${o} months`,
label: "Rolling window"
})
Now that we’ve read in our data and set up a filter, it’s time to sit back, relax, and enjoy the plot!
Code
.plot({
Plotcolor: {scheme: "BuRd"},
marks: [
.ruleY([0]),
Plot.dot(temps, {x: "Date", y: "Anomaly", stroke: "Anomaly"}),
Plot.lineY(temps, Plot.windowY(rolling, {x: "Date", y: "Anomaly"}))
Plot
] })
When you’re working with Observable inside of a Quarto notebook, you have all the same sorts of options you would when working with Python or R. In this case, we used //| code-fold: true
with our {ojs}
block to allow the user to hdie and show the code. Click ▶ Code
to reveal the code we used to embed the graph!
Learn more
Quarto’s documentation on using Observable is excellent, I definitely recommend checking it out. In the example above we specifically used Observable Plot, which you can learn more about on observablehq.com.