Skip to content

Is there any way (inside Plot) to add a legend to a plot with two lines derived from the same data? #2007

Answered by mythmon
llimllib asked this question in Q&A
Discussion options

You must be logged in to vote

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 like stroke: d => "length".

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: "cul…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@mythmon
Comment options

@llimllib
Comment options

@Fil
Comment options

Answer selected by llimllib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants