Public
Edited
Jun 10, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
caption: my_graph_equation,
aspectRatio: 1,
marks: [
Plot.linearRegressionY(data, {
x: "x",
y: "y",
stroke: "steelblue",
strokeWidth: 3,
fillOpacity: cb.includes("confidence band") ? 0.1 : 0
}),
Plot.rect(data, {
x1: "x",
y1: "y",
x2: (d) => d.x - Math.sign(my_graph.a) * (d.y - my_graph.predict(d.x)),
y2: (d) => my_graph.predict(d.x),
fill: "red",
fillOpacity: 0.15
}),
cb.includes("distance")
? Plot.ruleX(data, {
x: "x",
y1: "y",
y2: (d) => my_graph.predict(d.x),
stroke: "red"
})
: null,
Plot.dot(data, { x: "x", y: "y", r: 3, fill: "red" }),
Plot.text([my_graph_equation], {
x: x_center,
y: my_graph.predict(x_center),
rotate: angle,
dy: -12,
textAnchor: "middle",
fontSize: "16px",
stroke: "white",
fill: "steelblue"
})
]
})
Insert cell
my_graph = lR
.regressionLinear()
.x((d) => d.x)
.y((d) => d.y)(data)
Insert cell
my_graph_equation = `y = (${my_graph.a.toFixed(2)})x + ${my_graph.b.toFixed(2)}`
Insert cell
Insert cell
Insert cell
lR = require("d3-regression@1")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more