Code
//Import `aquero`, a `dplyr` equivalent in Observable:
import {aq, op} from "@uwdata/arquero" import {aq as aq, op as op} from "@uwdata/arquero"
Arvind V.
This post explores the use of Observable (OJS) for data visualization, particularly using the plot library. We will use the penguins dataset from the palmerpenguins package.
Let’s import and view the penguins dataset:
plot library in OJSLet’s create a slider for making variable histograms with facetting:
Put a interactive filter on the data:
Plot.rectY(filtered,
Plot.binX(
{y: "count"},
{x: "body_mass_g", fill: "species", thresholds: 20}
))
.plot({
title: "Facetted Histogram",
caption: "Figure 1. A chart with a title, subtitle, and caption.",
facet: {
data: filtered,
x: "sex",
y: "species",
marginRight: 80
},
marks: [
Plot.frame(),
]
}
)Plot.plot({
grid: true,
inset: 10,
//aspectRatio: 0.05,
color: {legend: true},
marks: [
Plot.frame(),
Plot.rectY(df_penguins,
Plot.binX({y: "count"}, {x: "body_mass_g"},
{color: "species"}, {fill: "species"})
),
Plot.ruleY([0])
],
title: "TITLE: Histogram of Body Mass of Penguins",
subtitle: "SUBTITLE: Using OJS",
caption: "Figure 1. A chart with a title, subtitle, and caption.",
})Figure 1 is a histogram.
Trying to master the syntax, step by step:
Plot.boxX(
// Data
df_penguins,
// Aesthetics
{x: "body_mass_g", y: "species", fill: "species", sort: {y: "x"}}
)
// Plot options, concatenated
.plot({
title: "Box Plot in Observable",
subtitle: "Getting hold of the Syntax",
caption: "Box Plot on Log scale",
height: 400,
marginLeft: 100,
// scales
x:{type:"log",}})Let’s try the same plot with facetting, AND with change in colour palette:
Plot.boxX(
// Data
df_penguins,
// Aesthetics
{x: "body_mass_g", y: "species", fy: "island",
fill: "species",
sort: {y: "x"},
}
)
// Plot options, concatenated
.plot({
title: "Box Plot in Observable",
subtitle: "Getting hold of the Syntax",
caption: "Box Plot on Log scale",
// scales
x:{type:"log"}})Failed to fetch
Plot.plot({
grid: true,
inset: 10,
color: {legend: true},
x: {labelAnchor: "center"},
y: {labelAnchor: "center"},
//title: "Scatter Plot for Penguins",
marginLeft: 40,
marks: [
Plot.frame(),
Plot.dot(penguins,
{x: "bill_length_mm", y: "bill_depth_mm",
stroke: "black", fill: "species", strokeWidth: 0.2})
]
})Plotaquero| species | count |
|---|---|
| Adelie | 152 |
| Gentoo | 124 |
| Chinstrap | 68 |