-
Notifications
You must be signed in to change notification settings - Fork 635
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.

