-
I put together an observable to explain what I mean: https://observablehq.com/d/8275ce1053510e0f Is there any way to use plot to generate a legend for these two lines? In the particular use case I'm working on, there's no way ahead of time to know how to place labels |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
There are two things that you'll need to change to get an automatic legend. First, you have to turn it on, by adding Doing those two things results in a final result of chart = Plot.plot({
color: {legend: true },
y: { label: "mm" },
marks: [
Plot.lineY(sortedpenguins, {
x: "flipper_length_mm",
y: "culmen_length_mm",
thresholds: 12,
stroke: d => "depth",
}),
Plot.lineY(sortedpenguins, {
x: "flipper_length_mm",
y: "culmen_depth_mm",
thresholds: 12,
stroke: d => "length",
})
]
}) which looks like this: I've sent a suggestion to your notebook with those changes. |
Beta Was this translation helpful? Give feedback.
There are two things that you'll need to change to get an automatic legend. First, you have to turn it on, by adding
color: {legend: true}
. Second, that won't work when you specify colors directly in your marks. Instead you have to let Plot figure those out. You can replace the stroke specifiers with something likestroke: d => "length"
.Doing those two things results in a final result of