How do I connect or link legends in a subplot?

Good morning!
So it is the first time I have seriously played with the Plotly library and would love to use it further in projects. I have been working with Plotly 4.5.6.900 in R 3.3.2 trying to get legends to work together as one. Currently I’m using the following to generate this image.

a <- plot_ly(data = oneDeal6, type="bar", x = ~ClosedYearQuarter,  y = ~Turns,  color = ~Family, colors = "GnBu", hoverinfo = 'text', 
           text = ~paste(Family,"\nTurns: ", round(Turns,2))) 
b <- plot_ly(data = oneDeal6, type="bar", x = ~ClosedYearQuarter, y = ~GP, color = ~Family, colors = "GnBu", hoverinfo = 'text', 
           text = ~paste(Family,"\nGP: ", percent(GP)))
c <- plot_ly(data = oneDeal6, type="bar", x = ~ClosedYearQuarter, y = ~AvgDiscount,  color = ~Family, colors = "GnBu",hoverinfo = 'text', 
           text = ~paste(Family,"\nAvg Discount: ", percent(AvgDiscount))) 
d <- subplot(a,b,c,nrows = 3, shareX =TRUE, shareY = TRUE)

As you can see it generates three separate instances of the legend that are not connected. So if I try to double click on any of the types it only highlights the bars from the legend that is residing to the graph as shown in the following image.

Is there any way that I can connect / group the three plots to one legend so that they work together as one? I know that it can be done because when I do the same thing with the ggplotly function it will do this as shown in the preceding image.

Thank you for any help or tips that can be provided.

Try a grouped legend: https://plot.ly/r/legend/#grouped-legend

Here is another way of doing it – https://github.com/ropensci/plotly/blob/28a1215c22438ca9f9aa0de2f5725fab060b6f3b/R/subplots.R#L33-L40

@cdr6934 Any luck? The example works fine when there is no grouping variable in plotly, but with color = ~Family grouped legends are not appropiate. If you managed to make it work, I would be appreciative if you could share your code!

@rubenaf No… Unfortunately I was not been able to get it to work. The solutions provided did not help me figure out the issue. I’ve since rewritten it in Bokeh till I can get it to work with Plotly.

The links provided above illustrate a way to do it. For example:

df <- data.frame(x = c("a","b","c"), y = c(2,3,2), y2 = c(4,2,4))

p1 <- plot_ly(df, type = 'bar', x = ~x, y = ~y, color = ~x, legendgroup = ~x)
p2 <- plot_ly(df, type = 'bar', x = ~x, y = ~y2, color =  ~x, legendgroup = ~x, showlegend = F)
subplot(p1, p2, nrows = 2)

Hello @cdr6934

I know this is a bit late, but I guess this will also do :

a <- plot_ly(data = oneDeal6, type="bar", x = ~ClosedYearQuarter,  y = ~Turns,  color = ~Family, colors = "GnBu", hoverinfo = 'text', 
           text = ~paste(Family,"\nTurns: ", round(Turns,2)),
legendgroup = 'group1', showlegend = TRUE) 
b <- plot_ly(data = oneDeal6, type="bar", x = ~ClosedYearQuarter, y = ~GP, color = ~Family, colors = "GnBu", hoverinfo = 'text', 
           text = ~paste(Family,"\nGP: ", percent(GP)),
legendgroup = 'group1', showlegend = FALSE)
c <- plot_ly(data = oneDeal6, type="bar", x = ~ClosedYearQuarter, y = ~AvgDiscount,  color = ~Family, colors = "GnBu",hoverinfo = 'text', 
           text = ~paste(Family,"\nAvg Discount: ", percent(AvgDiscount)),
legendgroup = 'group1', showlegend = FALSE) 
d <- subplot(a,b,c,nrows = 3, shareX =TRUE, shareY = TRUE)

For me, this (the solution of @ParisaM )is not 100% working. My plot will show a legend that contains only once the variable I stated at color which is Family here (for me it is: color=~location) (and i have 4 locations, so it only shows 4 locations). But selecting a specific trace makes them all dissapear instead of the one I click on. If I find a solution, I’ll state it. But i really hope somebody can help me haha.