plotly.js icon indicating copy to clipboard operation
plotly.js copied to clipboard

Support for multiple legends

Open cpsievert opened this issue 9 years ago • 36 comments

Useful when mapping multiple discrete variables to different visual aesthetics (i.e., marker.color & marker.symbol) without having to list every possible combination of levels.

Here is a ggplot2 example

qplot(data = mtcars, mpg, wt, color = factor(vs), shape = factor(am))

screen shot 2017-05-10 at 4 58 20 pm

cpsievert avatar May 10 '17 21:05 cpsievert

We had discussions a long time ago about an idea I was calling "legends 2.0" that would support this and many other use cases:

  • create a syntax for describing trace legend items (with modifications) within annotations
  • the legends we have today would then just be a special case of an annotation, but you could construct any arbitrary legend, or caption (symbols embedded in prose), or whatever, using this syntax.

For example, assuming the plot you've made above used one trace with variable color and symbol, its legend could be an annotation like (syntax off the top of my head... could be very different but you get the idea):

'<span style="font-size: 1.3em;">factor(vs)</span><br>' +
'&trace0{"marker.color":"red","marker.symbol":"circle"}; 0<br>' +
'&trace0{"marker.color":"green","marker.symbol":"circle"}; 1<br>' +
'<br>' +
'<span style="font-size: 1.3em;">factor(am)</span><br>' +
'&trace0{"marker.color":"black","marker.symbol":"circle"}; Automatic<br>' +
'&trace0{"marker.color":"black","marker.symbol":"triangle-up"}; Manual'

For the record, I don't know if other platforms implement a system like this but I'll note that this concept is inspired by a very similar system in Igor Pro.

alexcjohnson avatar May 16 '17 03:05 alexcjohnson

Are there any concrete plans to support multiple legends in the near future?

zichner avatar Jul 25 '17 15:07 zichner

Seconding this, it would be useful. On some data I'm currently working on, plotly (used in R through the corresponding package) outputs something like this:

It could be much simpler, plus it's not shown in the above picture but the factor levels are also concatenated between the two legend types being used, which is a bit ugly (it adds "()" and the spacing syntax is not correct).

Kabouik avatar Oct 11 '17 11:10 Kabouik

I third this! It could be nice to be able to have legends specific to a give subplot rather than having to list all items in one legend.

June-Skeeter avatar Dec 06 '17 04:12 June-Skeeter

Hopefully, this could be done also in javascript plotly!!

slfan2013 avatar Apr 22 '18 02:04 slfan2013

Has there been any progress on this?

micdonato avatar Aug 01 '18 00:08 micdonato

Looking forward to have this feature available.

vianamp avatar Sep 16 '18 17:09 vianamp

Is there any update on this?

slfan2013 avatar Sep 19 '18 16:09 slfan2013

This would be great for when there are multiple y axes, so that traces are more obviously linked to the relevant y axis.

teddyward avatar Oct 23 '18 23:10 teddyward

This would still be really great to have for subplots and for multiple y axes.

The grouping still doesn't work as a substitute, mostly because you cannot highlight individual traces, only legend groups.

russellromney avatar Dec 04 '18 04:12 russellromney

Is there any update on this? I would really like to know how to attach a legend on the right hand side of each subplot.

danny305 avatar Mar 28 '19 23:03 danny305

I was excited to see that this issue had been raised two years ago, hoping that it would be solved. Has there been any progress?

petersmp avatar Apr 11 '19 12:04 petersmp

Going to add another bump to this. I'm particularly interested in the interaction with ggplotly, since currently the lack of support for multiple legends changes the plot in unexpected ways.

Thomas-Hirsch avatar Jun 06 '19 14:06 Thomas-Hirsch

@alexcjohnson Can you shed a light on if giving legend groups an annotation/title is possible by now? So it basically would look like the mockup above where the legend groups have text above them.

Braintelligence avatar Jul 02 '19 12:07 Braintelligence

Indeed, it would be great to have multiple legends available ! It would also solve this issue with ggplotly #1164

maxibor avatar Jul 05 '19 13:07 maxibor

Is there any update. Thank you

lalitsc12 avatar Jul 12 '19 13:07 lalitsc12

This would be really handy. Is there an update?

TolganLight avatar Jul 16 '19 12:07 TolganLight

Agree with @russellromney Having a separate legend option for each subplot is a high priority issue! Having one legend entry on the side or at the bottom of the subplots is not that helpful as they do not know which points are including in each subplot until they hover over the lines. See this example of how useful the individual legends would be.

What is the timeline for adding this? Thanks! PlotlySubplotMultipleLegendsExample

JeroneGagliano avatar Nov 19 '19 14:11 JeroneGagliano

Agree.. as having legend on side with many plots confuses the user and also looks ugly.

lalitsc12 avatar Nov 19 '19 16:11 lalitsc12

Agree with @russellromney Having a separate legend option for each subplot is a high priority issue! Having one legend entry on the side or at the bottom of the subplots is not that helpful as they do not know which points are including in each subplot until they hover over the lines. See this example of how useful the individual legends would be.

What is the timeline for adding this? Thanks! PlotlySubplotMultipleLegendsExample

Very interested in this feature, so subscribing in this thread as well after the initial one here

https://github.com/ropensci/plotly/issues/1164#issuecomment-489665978

@jessicajturner this might be of interest to you too.

cgpu avatar Nov 20 '19 22:11 cgpu