Legend order in scatter plot ignored when frame animation is used
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 2.7k
- Forks
- 641
- PR merge metrics
- No merged PRs in 30d
Description
Hello everyone!
I'm currently working on a dashboard for monitoring in R and would like use Plotly's animated scatter plot, which works like a charm except for the legend order. Indepentent from how I define the order within the code, the order defaults to alphabetical.
Here's a code to reproduce the issue based on the gapminder dataset. To demonstrate the issue, I've reordered the continent factor variable by reversing the levels and it still shows in alphabetical order. The same happens, when I hardcode the order into the levels parameter.
library(tidyverse)
library(plotly)
library(gapminder)
gapdf <- gapminder
fig <- gapdf %>%
mutate(
continent = factor(continent, levels = rev(levels(continent)))
) %>%
plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
frame = ~year,
text = ~country,
hoverinfo = "text",
type = 'scatter',
fill = ~'',
mode = 'markers'
)
fig <- fig %>% layout(
xaxis = list(
type = "log"
)
)
fig <- fig %>% animation_opts(
1000, easing = "elastic", redraw = FALSE
)
fig <- fig %>% animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
fig <- fig %>% animation_slider(
currentvalue = list(prefix = "YEAR ", font = list(color="#CC0033"))
)
fig

When "frame" and its corresponding animation commands are excluded, the legend order works as intended:
library(tidyverse)
library(plotly)
library(gapminder)
gapdf <- gapminder
fig <- gapdf %>%
mutate(
continent = factor(continent, levels = rev(levels(continent)))
) %>%
plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
text = ~country,
hoverinfo = "text",
type = 'scatter',
fill = ~'',
mode = 'markers'
)
fig

Reversing the order is only possible through adjusting the traceorder of the legend, but I would like to use the order of the levels as provided by the data, which is not alphabetical or reversed alphabetical in 99,9% of my cases, so this workaround won't help.
If anyone knows a proper workaround, I would highly appreciate it!
Kind regards and have a nice day everyone.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided R reproduction with the gapminder dataset and compare the animated and non-animated plots. Trace how frame animation handles the continent factor levels; done means the animated scatter plot preserves the data-defined legend order without requiring a traceorder workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100