plotly / plotly/plotly.R

Legend order in scatter plot ignored when frame animation is used

Aperta
#2,000 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
R
Stelle
2.7k
Fork
641
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo la riproduzione R fornita con il dataset gapminder e confronta i grafici animato e non animato. Traccia il modo in cui l’animazione dei frame gestisce i livelli del fattore continent; il lavoro è completo quando il grafico a dispersione animato conserva l’ordine della legenda definito dai dati senza richiedere un workaround traceorder.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
r
Ambito
data-visualization
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.