title: Merging legends in plotly subplot
body: <p>I have several groups where for each I have several classes for which I measured continuous values:</p>
  <pre><code>set.seed(1)
  
  df &lt;- data.frame(value = c(rnorm(100,1,1), rnorm(100,2,1), rnorm(100,3,1),
                             rnorm(100,3,1), rnorm(100,1,1), rnorm(100,2,1),
                             rnorm(100,2,1), rnorm(100,3,1), rnorm(100,1,1)),
                   class = c(rep(&quot;c1&quot;,100), rep(&quot;c2&quot;,100), rep(&quot;c3&quot;,100),
                             rep(&quot;c2&quot;,100), rep(&quot;c4&quot;,100), rep(&quot;c1&quot;,100),
                             rep(&quot;c4&quot;,100), rep(&quot;c3&quot;,100), rep(&quot;c2&quot;,100)),
                   group = c(rep(&quot;g1&quot;,300), rep(&quot;g2&quot;,300), rep(&quot;g3&quot;,300)))
  
  df$class &lt;- factor(df$class, levels =c(&quot;c1&quot;,&quot;c2&quot;,&quot;c3&quot;,&quot;c4&quot;))
  df$group &lt;- factor(df$group, levels =c(&quot;g1&quot;,&quot;g2&quot;,&quot;g3&quot;))
  </code></pre>
  <p>Not each group in the data has the same classes, or put differently each group has a subset of all classes.</p>
  <p>I'm trying to generate <code>R</code> <code>plotly</code> density curves for each group, color-coded by class, and then combine them all to a single plot using <code>plotly</code>'s <code>subplot</code> function.</p>
  <p>This is what I'm doing:</p>
  <pre><code>library(dplyr)
  library(ggplot2)
  library(plotly)
  
  
  set.seed(1)
  
  df &lt;- data.frame(value = c(rnorm(100,1,1), rnorm(100,2,1), rnorm(100,3,1),
                             rnorm(100,3,1), rnorm(100,1,1), rnorm(100,2,1),
                             rnorm(100,2,1), rnorm(100,3,1), rnorm(100,1,1)),
                   class = c(rep(&quot;c1&quot;,100), rep(&quot;c2&quot;,100), rep(&quot;c3&quot;,100),
                             rep(&quot;c2&quot;,100), rep(&quot;c4&quot;,100), rep(&quot;c1&quot;,100),
                             rep(&quot;c4&quot;,100), rep(&quot;c3&quot;,100), rep(&quot;c2&quot;,100)),
                   group = c(rep(&quot;g1&quot;,300), rep(&quot;g2&quot;,300), rep(&quot;g3&quot;,300)))
  
  df$class &lt;- factor(df$class, levels =c(&quot;c1&quot;,&quot;c2&quot;,&quot;c3&quot;,&quot;c4&quot;))
  df$group &lt;- factor(df$group, levels =c(&quot;g1&quot;,&quot;g2&quot;,&quot;g3&quot;))
  
  plot.list &lt;- lapply(c(&quot;g1&quot;,&quot;g2&quot;,&quot;g3&quot;), function(g){
    density.df &lt;- do.call(rbind,lapply(unique(dplyr::filter(df, group == g)$class),function(l)
      ggplot_build(ggplot(dplyr::filter(df, group == g &amp; class == l),aes(x=value))+geom_density(adjust=1,colour=&quot;#A9A9A9&quot;))$data[[1]] %&gt;%
        dplyr::select(x,y) %&gt;% dplyr::mutate(class = l)))
    plot_ly(x = density.df$x, y = density.df$y, type = 'scatter', mode = 'lines',color = density.df$class) %&gt;%
      layout(title=g,xaxis = list(zeroline = F), yaxis = list(zeroline = F))
  })
  subplot(plot.list,nrows=length(plot.list),shareX=T)
  </code></pre>
  <p>Which gives:
  <a href="https://i.stack.imgur.com/LiZwW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LiZwW.png" alt="enter image description here" /></a></p>
  <p>The problems I'd like to fix are:</p>
  <ol>
  <li>Have the legend appear only once (right now it repeats for each group) merging all classes</li>
  <li>Have the title appear in each of the subplots rather than only for the last plot at is it is now. (I know that I could simply have the group name as the x-axis titles but I'd rather save that space because in reality I have more than 3 groups)</li>
  </ol>