question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

line-and-point legends

See original GitHub issue

I need a Line Chart with Points plots and there is an example. It states that altair.Chart(data).mark_line(point=True) will produce line chart with points.

Since I have multiple series to plot I specified color='kind', strokeDash='kind' (or both). I found that the legends are only circles.

  • With color='kind', the circles are filled with the colors.
  • With strokeDash='kind', the boundary of the circles are solid or dashed lines.

Without point=True I have lines and the effects are that lines are colored or dashed. With both point=True and {color='kind', strokeDash='kind', or both} I would expect the legends to have both lines and circles, same appearance as plot. I couldn’t figure out how to achieve the desired behavior so that I am opening this issue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
joelostblomcommented, Mar 23, 2022

Thanks for providing an example @majiang! Currently it is not possible to combine legends of points and lines, you can follow the discussion here https://github.com/altair-viz/altair/issues/1785 and upstreams in vega-lite here https://github.com/vega/vega-lite/issues/5996

If you append .resolve_scale(color='independent', strokeDash='independent') to your chart, you can get something like this:

image

The title can be removed from one of the legends by setting title='' for that encoding channel.

0reactions
majiangcommented, Mar 23, 2022

Sorry for not being able to try and feedback that time.

I now think it is impossible due to vega-lite’s limitation and describe it with examples.

The desired output was something like image

which I created with Excel. By “line-and-point legend” I meant line legend and point legend combined as they look.

With altair, when I opened this issue,

def sample_data():
    return pandas.DataFrame({
        "x": [0, 1, 2, 3, 0, 1, 2, 3],
        "y": [0, 1, 2, 3, 3, 2, 1, 0],
        "kind": ["a", "a", "a", "a", "b", "b", "b", "b"],
    })

altair.Chart(sample_data()).mark_line(point=True).encode(
    x="x", y="y", color="kind", strokeDash="kind")

produced

visualization (3)

the main part of which was satisfactory, but only legend was not.

I have also tried

(
    altair.Chart(sample_data()).mark_point().encode(
        x="x", y="y", color="kind"
    )
    +
    altair.Chart(sample_data()).mark_line().encode(
        x="x", y="y", color="kind", strokeDash="kind"
    )
)

which produces similar result:

visualization (4)

Here I don’t mind whether the circles are filled or not.

Since the appearance of legend of a Chart is determined fully automatically by vega-lite from the definition of encoding channels, we can’t produce the desired output.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Legend with both point and line in R - Stack Overflow
What I actually want in the legend is to make the point and the line side-by-side instead of on top of each other,...
Read more >
Different Colors of Points & Lines in Base R ... - Statistics Globe
In this R tutorial you'll learn how to display the same legend item with different colors of points and lines. The table of...
Read more >
Legend of mixed line and point plots - Google Groups
Dear all, I would like to plot a mixture of lines and points in a plot. Well, it works however I think the...
Read more >
Is there an easier way to have point and line legend labels ...
Is there an wasier way to have point and line legends group together for the same group level (so the 'a' legends are...
Read more >
Legends (ggplot2) - Cookbook for R
You want to modify the legend of a graph made with ggplot2. ... scale_shape_xxx instead of scale_fill_xxx . colour maps to the colors...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found