state_plot = Plot.plot({
grid: false,
margin: 40,
height: 800,
x: {
label: "Inflation adjusted per capita spending on policing →",
tickFormat: d3.format("$,.0f")
},
y: {
label: "↑ Percent of reported crimes cleared by law enforcement"
},
marks: [
Plot.line(state_data, {
x: "per_cap_spending",
y: state_rate,
curve: "catmull-rom"
}),
Plot.dot(state_data, {
x: "per_cap_spending",
y: state_rate,
fill: "currentColor",
title: (d) =>
`${d.year} \n
${dollar_format(
d.per_cap_spending
)} per capita spending (inflation adjusted) \n
${percent_format(d[state_rate])} clearance rate for ${state_rate_input}`
}),
Plot.text(state_data, {
filter: (d) => (d.year % 5 === 0) & ((d.year != 2010) & (d.year != 2005)),
x: "per_cap_spending",
y: state_rate,
text: (d) => `${d.year}`,
dy: -10,
dx: 17
}),
Plot.text(state_data.slice(36), {
x: "per_cap_spending",
y: state_rate,
text: ["2021"],
dy: -8,
dx: 17
}),
Plot.text(state_data.slice(20), {
x: "per_cap_spending",
y: state_rate,
text: ["2005"],
dy: -8,
dx: -22
}),
Plot.text(state_data.slice(25), {
x: "per_cap_spending",
y: state_rate,
text: ["2010"],
dy: -12,
dx: 0
}),
Plot.text(state_data.slice(29), {
x: "per_cap_spending",
y: state_rate,
text: ["2014"],
dy: -10,
dx: 16
}),
Plot.axisY({
tickFormat: d3.format(".0%"),
interval: 0.01
}),
Plot.text(state_data.slice(13), {
x: "per_cap_spending",
y: state_rate,
text: [text1],
dx: 250
}),
Plot.text(state_data.slice(13), {
x: "per_cap_spending",
y: state_rate,
text: [text2],
dx: 20,
dy: 500
})
]
})