plotly / plotly/plotly.R

Legend order in scatter plot ignored when frame animation is used

Ouverte
#2,000 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
R
Étoiles
2.7k
Forks
641
Métriques de merge des PR
Aucune PR mergée en 30 j

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

grafik

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

grafik

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par exécuter la reproduction R fournie avec le jeu de données gapminder et comparez les graphiques animé et non animé. Suivez la manière dont l’animation des frames gère les niveaux du facteur continent ; le travail est terminé lorsque le nuage de points animé préserve l’ordre de la légende défini par les données sans nécessiter de workaround traceorder.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
r
Domaine
data-visualization
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
30/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.