plotly / plotly/plotly.R

`guide_legend(reverse = TRUE)` does not reverse legend order

Open
#2,246 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

I reversed the legend order with guide_legend(reverse = TRUE), but the legend is unchanged in the plotly plot. The ggplot version is reversed.

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

Originally posted by @simonsteiger in https://github.com/plotly/plotly.R/issues/2234#issuecomment-1457010264

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied R example and comparing the ggplot legend with the result of ggplotly(p2). Trace the ggplotly conversion path for guide_legend(reverse = TRUE), then verify that the converted plot preserves the reversed legend order.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.