Issue with NA handling in scatter plots: Two NAs per category cause incorrect line connection
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- R
- Star
- 2.7k
- Fork
- 641
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách chạy ví dụ plot_ly() có thể tái hiện trong issue và so sánh đầu ra khi có và không có hovertemplate và split = ~Category. Theo dõi cách lệnh gọi R tới plotly biểu diễn các giá trị NA và các trace theo category, sau đó xác minh rằng chính xác hai NA cho mỗi category tạo ra một khoảng trống hiển thị được mà không cần split hoặc loại bỏ hovertemplate.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- r
- Lĩnh vực
- data-visualization
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100