Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug: ggplotly() no longer removes legends #2234

Merged
merged 3 commits into from
Feb 13, 2023
Merged

Fix bug: ggplotly() no longer removes legends #2234

merged 3 commits into from
Feb 13, 2023

Conversation

trekonom
Copy link
Contributor

@trekonom trekonom commented Feb 9, 2023

This PR proposes a fix for a bug introduced with #2067 and thereby closes #2212.

Specifically #2067 added the line

aes_no_guide <- names(vapply(p$guides, identical, logical(1), "none"))

to take care of guides(aes = "none"). However, this has the unwanted effect to remove legends for all aesthetics mentioned in guides() as can be seen from the following reprex which should show a color legend:

library(ggplot2)
library(plotly, warn=FALSE)

p <- ggplot(mtcars, aes(hp, mpg, color = factor(cyl))) +
  geom_point() +
  guides(color = guide_legend())

aes_no_guide <- names(vapply(p$guides, identical, logical(1), "none"))

aes_no_guide
#> [1] "colour"

ggplotly()

The PR fixes that by including only those aes for which vapply(p$guides, identical, logical(1), "none")) is TRUE:

aes_no_guide <- names(p$guides)[vapply(p$guides, identical, logical(1), "none")]

aes_no_guide
#> character(0)

Created on 2023-02-09 with reprex v2.0.2

@cpsievert
Copy link
Collaborator

Awesome, thank you!

@simonsteiger
Copy link

simonsteiger commented Mar 5, 2023

While working with guides() and ggplotly(), I realised that the legend for those arguments specified in guides() disappears.

This seems to be despite @trekonom's fix above.

Running on
plotly 4.10.1.9000
ggplot2 3.4.1

library(plotly)
library(ggplot2)

df <- data.frame(x = rnorm(10), y = rnorm(10), group = rep(c("A", "B"), times = 5))

# Plot without guides
p1 <- ggplot(df, aes(x = x, y = y, color = group)) +
  geom_line()

# Plot with guides
p2 <- ggplot(df, aes(x = x, y = y, color = group)) +
  geom_line() +
  guides(color = guide_legend())

# Convert with guides
ggplotly(p1)
# Legend shown

# Convert without guides
ggplotly(p2)
# Legend not shown

@trekonom
Copy link
Contributor Author

trekonom commented Mar 5, 2023

Unfortunately I could not reproduce your issue (Could you check your code after re-installing the plotly dev version?).

Running your code with the dev version gives me a legend for both cases:

library(plotly, warn=FALSE)
#> Loading required package: ggplot2

set.seed(123)

df <- data.frame(x = rnorm(10), y = rnorm(10), group = rep(c("A", "B"), times = 5))

# Plot without guides
p1 <- ggplot(df, aes(x = x, y = y, color = group)) +
  geom_line()

# Plot with guides
p2 <- ggplot(df, aes(x = x, y = y, color = group)) +
  geom_line() +
  guides(color = guide_legend())

# Convert with guides
ggplotly(p1)

# Legend shown

# Convert without guides
ggplotly(p2)

# Legend not shown

Created on 2023-03-05 with reprex v2.0.2

@simonsteiger
Copy link

Thank you for your comment and apologies – you are correct, the legend is shown.

I initially discovered the "missing legend" issue when I reversed the legend order with guide_legend(reverse = TRUE) and then used ggplotly().
With your fix, I've now tried to reverse the legend order with guide_legend(reverse = TRUE), but the legend remains in the original order for the plotly plot. The ggplot version is reversed.

Since this is not related to missing legends anymore, I will open a new issue on reversing the legend.

library(plotly, warn=FALSE)
#> Loading required package: ggplot2

set.seed(123)

df <- data.frame(x = rnorm(10), y = rnorm(10), group = rep(c("A", "B"), times = 5))

p1 <- ggplot(df, aes(x = x, y = y, color = group)) +
  geom_line() +
  guides(color = guide_legend())
# Legend order default

# Plot with guides
p2 <- ggplot(df, aes(x = x, y = y, color = group)) +
  geom_line() +
  guides(color = guide_legend(reverse = TRUE))
# Legend order reversed

# Convert ggplot with reversed legend order
ggplotly(p2)
# Legend not reversed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plotly code using ggplot2 no longer working (legend removed)
3 participants