Grouping by color and/or symbol changes the order of error_y bars
还没有人认领这个 Issue。
- 主要语言
- R
- 星标
- 2.7k
- 派生
- 641
- PR 合并指标
- 30 天内没有已合并 PR
描述
Reopening issues #762 and #1110, this is still happening in 2019 and maybe I found the cause.
Briefly, error bars displayed by error_x and error_y appear in the wrong order when grouping data by color or (as I found) symbol. Recycling @Cristoforetti 's code from #1110:
df<-data.frame("X"=c(1:20),
"Y"=c(1:20),
"SD"=c(1:20),
"G"=c(rep("A",3),rep("B",5),rep("C",4),rep("D",5),rep("A",3)),
"g"=c(rep("a",2),rep("b",5),rep("c",4),rep("d",5),rep("e",4))
)
# no grouping; error bars correct
p1<-plot_ly(df,
x=~X,
y=~Y,
type="scatter",
mode="markers",
error_y =list(
array=~SD,
thickness=1
)
)
# grouping by color; error bars wrong
p2<-plot_ly(df,
x=~X,
y=~Y,
color=~G,
type="scatter",
mode="markers",
error_y =list(array=~SD,
thickness=1
)
)
subplot(p1,p2)

I found that the correct behaviour (error bars associated with the correct data points) can be restored by passing a version of the input dataframe order-ed by the color column:
# ordering the input data frame by the color column yields the correct behaviour
p3<-plot_ly(df[order(df$G),],
x=~X,
y=~Y,
color=~G,
type="scatter",
mode="markers",
error_y =list(array=~SD,
thickness=1
)
)
subplot(p1,p2,p3)

The same happens when ordering by symbol: the error bars are screwed up unless df is order-ed by the symbol column. When using both color and symbol, one must order by both the color column and the symbol column in this order:
# using "G" for color and "g" for symbols; ordering by G, then g yields the correct behaviour
p4<-plot_ly(df[order(df$G,df$g),],
x=~X,
y=~Y,
symbol=~g,
color=~G,
type="scatter",
mode="markers",
error_y =list(array=~SD,
thickness=1
)
)
# ordering by g, then G yields the wrong behaviour
p5<-plot_ly(df[order(df$g,df$G),],
x=~X,
y=~Y,
symbol=~g,
color=~G,
type="scatter",
mode="markers",
error_y =list(array=~SD,
thickness=1
)
)
subplot(p4,p5)

What seems to be happening here is that color and symbol are reordering the (copy of) df handled by plotl_ly, but for some reason the reordering only affects the columns identified by x, y, color and symbol; as a consequence, the column used by error_y is now in the wrong order, and error bars are associated with the wrong data points. Ordering the df prior to calling plot_ly, or passing an ordered version of it, solves the issue. Still, it would be great to see this fixed in future versions of plotly.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,使用提供的数据框运行可复现的 plot_ly 和 subplot 示例,比较分组输入和预先排序的输入。跟踪颜色和符号的分组行为,并验证 error_y 值仍与其原始点保持关联;当误差条在不需要手动排序的情况下仍能正确对齐时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- plotly, r
- 领域
- data-visualization
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100