Skip to content

ggplotly() is unable to merge legend entries across layers #2010

Description

@cboettig

Thanks for an amazing package! The mapping between plotly and ggplot is particularly impressive and very powerful!

However, ggplotly fails to reproduce the appropriate legends generated by ggplot in non-trivial facet_wrap cases. After discussion on stackoverflow some of this behavior appears to be a bug.

Consider the following minimal reprex:

library(plotly)
library(ggplot2)

p <- mpg %>% 
  ggplot(aes(year)) +
  geom_ribbon(aes(ymin=cty, ymax=hwy, fill = manufacturer), alpha=0.2) + 
  geom_line(aes(y = hwy, col=manufacturer))  +
  facet_wrap(~class)
p
plotly::ggplotly(p)

Note that (a) we get too many plotly legend entries, duplicating the legend for each facet, due to legendgroup not being correctly handled. Also note (b) the legends we do and don't get depend on the order (do we do geom_ribbon first or geom_line first). The bug does not appear if we have only one geom, and the missing legends continue as we add more geoms.

Perhaps not related -- but the interactive zoom options in faceted plots are not entirely intuitive either, i.e. it's not possible to zoom the whole window on a single facet plot. (Perhaps the take home is that it might be better to render facet plots as more independent objects in plotly)?

Activity

  1. cpsievert commented on Nov 2, 2021

    @cpsievert
    Collaborator

    Now that #2067 has been merged, you can at least workaround this issue by doing p <- p + guide(color = "none").

    I will leave this open though since ggplotly() still isn't smart enough to merge legend entries across layers in the same way that ggplot2 does

  2. changed the title [-]Bug in legend generation in some facet_wrap cases[/-] [+]ggplotly() is unable to merge legend entries across layers[/+] on Nov 2, 2021
  3. elcortegano commented on Dec 12, 2021

    @elcortegano

    Just asking, does the inability of ggplotly() to merge legend entries across layers mean that issue #2043 is not solved (despite being closed)? I am having a similar problem as in that issue, with legend entries for color and linetype being put together in ggplotly() (and expanding the items therein), while it looking ok in ggplot2. plotly version is 4.10.0.

  4. TheRocinante-lab commented on Jul 23, 2022

    @TheRocinante-lab

    I have been following the comments and as far as I understand ggplotly() is still unable to separate the labels as ggplot does, so to me this has not been solved yet. At least, I didn't find any solution. Please let me know if there is an example somewhere. Thanks

  5. added a commit that references this issue on Feb 29, 2024
  6. DarioS commented on Mar 12, 2024

    @DarioS

    I have another simpler example where it doesn't generate the desired appearance. Is it technically fixable or not?

    library(plotly) # version 4.10.4
    mtcars$am <- factor(mtcars$am)
    mtcars$vs <- factor(mtcars$vs)
    p <- ggplot(mtcars, aes(x = wt, y = drat, colour = vs, alpha = am)) + geom_point() +
          scale_alpha_manual(values = c(0.5, 1.0), breaks = c(0, 1))
    p  # Two legends    
    p + guides(alpha = "none") # One legend. Alpha is 1.
    ggplotly() # One legend. Alpha is 0.5 but should be 1.

    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions