Legend order in scatter plot ignored when frame animation is used
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- R
- Estrellas
- 2.7k
- Forks
- 641
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza ejecutando la reproducción proporcionada en R con el conjunto de datos gapminder y compara los gráficos animado y no animado. Rastrea cómo la animación de frames gestiona los niveles del factor continent; el trabajo estará terminado cuando el gráfico de dispersión animado conserve el orden de la leyenda definido por los datos sin requerir un workaround de traceorder.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- r
- Área
- data-visualization
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 30/100