plotly / plotly/plotly.R

Legend order in scatter plot ignored when frame animation is used

未关闭
#2,000 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
R
星标
2.7k
派生
641
PR 合并指标
30 天内没有已合并 PR

描述

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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先,使用 gapminder 数据集运行提供的 R 复现,并比较有动画和无动画的图。跟踪帧动画如何处理 continent 因子的各个水平;当动画散点图无需 traceorder workaround 即可保留由数据定义的图例顺序时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
r
领域
data-visualization
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。