Problems with barcharts with barmode='overlay' and Date objects
还没有人认领这个 Issue。
- 主要语言
- R
- 星标
- 2.7k
- 派生
- 641
- PR 合并指标
- 30 天内没有已合并 PR
描述
I need to plot several barcharts with Date objects on the x-axis with barmode = 'overlay' for a whole year. When there are enough values to plot, everything works fine, as I would like the bars to have equal widths along all traces.
But when one trace has few elements, the width of those traces are expanded.
And when one trace only has 1 value, it is not shown at all.
I could overcome those problems by creating fictional Dates with 0 y-values for the rest of the year, but I was hoping there is a better way of solving that.
The variable n in the example can be used to determine the amount of bars for the problematic
trace.
library(plotly)
n = 3
## All days of 2019 ###############
Days2019 <- data.frame(
date = seq(as.Date("2019-01-01"), as.Date("2019-12-31"), by = "days")
)
## Sample n Days of 2019 ###############
df <- data.frame(
date = sort(sample(Days2019$date, size = n, replace = F)),
val = sample(1:100, n, replace = T)
)
## Merge fictional 0 y-Values #################
# df = merge(df, Days2019, by="date", all=T)
# df[is.na(df$val),]$val=0
## Sample 30 Days of 2019 ###############
holiday1 <- data.frame(
date = sort(sample(Days2019$date, size = 30, replace = F))
)
## Plot Plotly ###############
maxY = max(df$val)
plot_ly() %>%
add_trace(data = holiday1, x = ~date, y = maxY, marker = list(color = "gray"), type="bar", name="Holidays") %>%
add_trace(data = df, x = ~date, y = ~val, marker = list(color = "red"), type="bar", name="Values") %>%
plotly::layout(autosize = F,
barmode = 'overlay',
bargap = 0.05,
xaxis = list(
type = "date",
range = c(as.Date("2019-01-01")-1,
as.Date("2019-12-31")+1),
autorange = FALSE,
tickmode = "auto",
nticks = 14L,
automargin = F)
)
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先运行提供的 R 示例并设置 n=3,然后改变 n,比较两条 trace 之间在 overlay 模式下的条形宽度和可见性。跟踪此示例所使用的 plotly.R 对 bar 和 Date 的处理,并在稀疏的 Date trace 保持适当宽度且单个值无需虚构的零值日期即可渲染时,视为 issue 完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- r
- 领域
- data-visualization
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100