observablehq / plot

A concise API for exploratory data visualization implementing a layered grammar of graphics
https://observablehq.com/plot/
ISC License
4.16k stars 171 forks source link

Null percent plots as zero #2072

Closed yurivish closed 1 month ago

yurivish commented 1 month ago

Plotting data with nulls normally causes those data points to be omitted from the plot. But if you use a percent channel option, it will plot them at zero.

https://observablehq.com/@yurivish/null-percent

I'm not entirely sure this is a bug but the behavior does seem quite surprising.

mbostock commented 1 month ago

Yep that’s a bug. The percent transform is currently (x) => x * 100 but I agree it should preserve nulls or coerce them to NaN rather than zero. As a workaround, you can represent values as NaN or undefined instead of null and it will work, or could set the transform option explicitly to something like (x) => x == null ? NaN : x * 100.

Plot.dot(time, { x: time, y: value }).plot({ y: { percent: true, transform: (x) => x == null ? NaN : x * 100 } })