render({
data: { values: birds },
transform: [
{
aggregate: [
{ op: "mean", field: "bill_length", as: "mean_length" },
{ op: "stderr", field: "bill_length", as: "se_length" },
{ op: "count", as: "sample_size" }
],
groupby: ["condition"]
},
{
calculate: "datum.mean_length - datum.se_length",
as: "lower_se"
},
{
calculate: "datum.mean_length + datum.se_length",
as: "upper_se"
}
],
layer: [
{
mark: {
type: "bar",
color: "#4c78a8",
opacity: 0.7,
width: 60
},
encoding: {
x: {
field: "condition",
type: "N",
title: "Condition",
axis: { labelFontSize: 12 }
},
y: {
field: "mean_length",
type: "Q",
title: "Average Beak Length (mm)",
scale: { zero: false }
},
tooltip: [
{ field: "condition", type: "N", title: "Condition" },
{ field: "mean_length", type: "Q", title: "Mean Beak Length", format: ".2f" },
{ field: "se_length", type: "Q", title: "Standard Error", format: ".3f" },
{ field: "sample_size", type: "Q", title: "Sample Size" }
]
}
},
{
mark: {
type: "rule",
color: "black",
strokeWidth: 2
},
encoding: {
x: { field: "condition", type: "N" },
y: { field: "lower_se", type: "Q" },
y2: { field: "upper_se", type: "Q" }
}
},
{
mark: {
type: "tick",
color: "black",
thickness: 2,
size: 10
},
encoding: {
x: { field: "condition", type: "N" },
y: { field: "lower_se", type: "Q" }
}
},
{
mark: {
type: "tick",
color: "black",
thickness: 2,
size: 10
},
encoding: {
x: { field: "condition", type: "N" },
y: { field: "upper_se", type: "Q" }
}
}
],
width: 300,
height: 250,
title: {
text: "Average Beak Length by Condition",
subtitle: "Error bars represent ± 1 Standard Error",
fontSize: 14,
anchor: "start"
}
})