plotly / plotly/plotly.R

Dynamic Axis Labels (Ticks) Misalignment with Data Points and Rangeslider

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

还没有人认领这个 Issue。

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

描述

Description:

In my Plotly chart with a rangeslider, I'm facing a dilemma with axis labels (ticks). Without using tickvals, the chart incorrectly displays Sunday as the start of the week. When using tickvals to correct this, the axis labels don't dynamically adjust when zooming out, leading to overcrowding.

Expected Behavior:

  • The x-axis should consistently display Monday as the start of each week.
  • When zooming out, the axis should dynamically adjust to a coarser interval (e.g., every month, every quarter, or every year) to maintain readability.
  • The rangeslider should allow smooth navigation while preserving appropriate tick density at different zoom levels.

Actual Behavior:

  1. Without tickvals:
  • The chart incorrectly uses Sunday as the start of the week.

  • Zooming functions work as expected, with appropriate tick density at different zoom levels.

Image

  1. With tickvals:
  • The chart correctly displays Monday as the start of the week.

  • When zooming out, the axis doesn't adjust to a more appropriate interval, resulting in overcrowded and illegible labels.

Image

Reproducible Example:

library(shiny)
library(plotly)
library(lubridate)
library(dplyr)

# Example data
data <- data.frame(
  date = seq(as.Date("2024-01-01"), Sys.Date(), by = "day"),  
  value = runif(length(seq(as.Date("2024-01-01"), Sys.Date(), by = "day")), 10, 100)  
)

# Aggregate data by week
data_weekly <- data %>%
  mutate(week = floor_date(date, unit = "week", week_start = 1)) %>%
  group_by(week) %>% 
  summarise(value = sum(value)) 

# Create a plot with the week start as the x-axis
fig <- plot_ly(data = data_weekly,
               x = ~week,  
               y = ~value,  
               type = 'bar') %>%
  layout(xaxis = list(
    range = c(floor_date(max(data$date) - months(3) + days(4),
                         unit = "week", week_start = 1),  # Set the x-axis range starting 3 months before the latest date
              max(data$date + days(4))),  # Extend the range to the maximum date (with 4 extra days for buffer)
    tickvals = data_weekly$week  # Use the start of each week as the ticks on the x-axis
  )
  )


fig %>%
  config(displayModeBar = FALSE) %>%
  layout(xaxis = list(rangeslider = list(visible = TRUE)))  # Add a range slider for the x-axis

Additional Notes:

  • The issue arises from the need to use tickvals to enforce Monday as the week start, which then prevents Plotly's automatic tick adjustment.
  • An ideal solution would combine correct week start representation (Monday) with dynamic tick adjustment when zooming.

System info

R version: 4.3.3 (2024-02-29)
Plotly version: 4.10.4
Operating System: Linux 5.15.173.1-1.cm2

贡献指南

打开贡献指南

从这里开始

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

调研方向

从使用 plotly、lubridate、dplyr、tickvals 和 x-axis rangeslider 的可复现 R 示例开始。研究固定的每周 tick 值如何与由缩放驱动的 tick 调整交互;当基于星期一的每周标签在缩放改变间隔以避免过度拥挤时仍保持正确,即视为完成。

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

评估

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

把新 issue 发到你的邮箱

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