No way to make legend order exactly match bar order in relative stacked bar plot
未关闭
还没有人认领这个 Issue。
- 主要语言
- R
- 星标
- 2.7k
- 派生
- 641
- PR 合并指标
- 30 天内没有已合并 PR
描述
Sometimes we want to plot negative values below x axis in a stacked bar plot. This can be done with barmode as "relative".
Further, I want to make the legend order exactly match the bar order in plot. However the positive bars stack from middle to top, negative bars stack from middle to down, thus no matter how I arrange the variable levels, there is no way to make the legend order exactly match bar order.
library(plotly)
library(data.table)
set.seed(42)
dt_test <- data.table(x = rep(1:5, each = 6), variable = rep(letters[1:6], 5), value = sample(10:40, 30))
dt_test[variable %in% letters[3:5], value := -value]
dt_test[, variable := factor(variable, levels = c("c", "d", "e", "a", "b", "f"))]
plot_ly(dt_test) %>%
add_bars(x = ~x, y = ~value, color = ~variable) %>%
layout(barmode = 'relative', legend = list(traceorder = "reversed"))

I'm not sure how this can be done, unless we
- change the stack order in negative part as bottom up
- or can arrange legend order freely without changing bar stack order
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先使用 plot_ly()、add_bars()、barmode = "relative" 和当前的图例设置重现所提供的 R 示例。调查是否可以支持负堆叠和正堆叠的所需行为;当图例顺序与可见的条形顺序完全一致,或已记录该限制和所选择的排序行为时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- r
- 领域
- data-visualization
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100