plotly / plotly/plotly.R

Issue with NA handling in scatter plots: Two NAs per category cause incorrect line connection

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

还没有人认领这个 Issue。

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

描述

Issue Summary

When using plot_ly() in R with a scatter plot (mode = "lines+markers"), missing (NA) values are expected to create gaps in the line plot. However, if exactly two NA values exist per category, the missing values are incorrectly connected by a line instead of creating a gap.

Interestingly, when the hovertemplate is removed, the line plot behaves as expected (i.e., creating a gap for NA values). This issue only occurs when there are exactly two NA values per category; the code works with any other number of NA values.

Additional Discovery:
The issue is resolved if I include the argument split = ~Category, but I cannot find documentation for split in Plotly, which makes me think it may be deprecated. Moreover, when the hovertemplate is removed, the inclusion of split does not work as expected and does not resolve the issue.

Reproducible Example

The following R code demonstrates the issue:

library(plotly)

df <- data.frame(
  Category = rep(c("A", "B"), each = 6),
  Date = c(2020, 2021, 2022, 2023, 2024, 2025, 2020, 2021, 2022, 2023, 2024, 2025),
  Value = c(10, 15, NA, NA, 20, 25, 12, 14, NA, 22, NA, 27)
)

df$Date <- factor(df$Date, levels = unique(df$Date), ordered = TRUE)

plot_ly(
  df,
  x = ~Date,
  y = ~Value,
  color = ~Category,
  type = 'scatter',
  mode = 'lines+markers',
  text = ~Category,
  hovertemplate = paste0("Date: %{x}<br>Category: %{text}")
)

Expected Behaviour

  • NA values should create a gap in the line plot, i.e., they should not be connected.
  • This works correctly when there is any number of NA values other than exactly two in any category.

Actual Behavior

  • When there are exactly two NA values per category, the missing values are incorrectly connected by a line instead of creating a gap.
  • When the hovertemplate is removed, the lines create a gap as expected.
  • The issue only arises when there are exactly two NA values per category; any other instance of NA works fine.
  • Including split = ~Category resolves the issue, but:
    • I cannot find any Plotly documentation on split, leading me to believe it may be deprecated.
    • Interestingly, when the hovertemplate is removed, including split does not resolve the issue.

Additional Notes
This seems to be an issue specifically triggered by the combination of NA handling and the hovertemplate. I would appreciate further insight on why this happens or suggestions for a workaround to preserve the gap in the case of two NA values.

System Info

R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows Server 2019 x64 (build 19045)

Matrix products: default

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先运行 issue 中可复现的 plot_ly() 示例,并比较使用和不使用 hovertemplate 及 split = ~Category 时的输出。跟踪 R 的 plotly 调用如何表示 NA 值和类别 trace,然后验证每个类别恰好有两个 NA 时会产生可见间隔,而无需使用 split 或移除 hovertemplate。

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

评估

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

把新 issue 发到你的邮箱

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