You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Plotly折线图X轴以周模式显示日期而非月份?

如何将Plotly折线图X轴从月份改为周单位显示

我拥有一份按周模式统计的数据集,已使用Plotly创建折线图。请问如何将X轴的显示从月份改为以周为单位?附数据集及当前代码如下:

w<-structure(list(Date = structure(c(1582329600, 1582934400, 1583539200, 
1584144000, 1584748800, 1585353600, 1585958400, 1586563200, 1587168000, 
1587772800, 1588377600, 1588982400, 1589587200, 1590192000, 1590796800, 
1591401600, 1592006400, 1592611200, 1593216000, 1593820800, 1594425600, 
1595030400, 1595635200, 1596240000, 1596844800, 1597449600, 1598054400, 
1598659200, 1599264000, 1599868800, 1600473600, 1601078400, 1601683200, 
1602288000, 1602892800, 1603497600, 1604102400, 1604707200, 1605312000, 
1605916800, 1606521600, 1607126400, 1607731200, 1608336000, 1608940800, 
1609545600, 1610150400, 1610755200, 1611360000, 1611964800), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), `Actual-retail and recreation` = c(3.57142857142857, 
6.85714285714286, 8.85714285714286, 4.57142857142857, -22.7142857142857, 
-41.5714285714286, -42, -43.7142857142857, -44.2857142857143, 
-40.1428571428571, -35, -30.1428571428571, -28.8571428571429, 
-25, -23.4285714285714, -19.1428571428571, -16.4285714285714, 
-14.2857142857143, -15.2857142857143, -13.7142857142857, -15.7142857142857, 
-14.7142857142857, -15.2857142857143, -14.5714285714286, -13.8571428571429, 
-14.7142857142857, -15.1428571428571, -15.8571428571429, -13.4285714285714, 
-15.7142857142857, -15, -15.2857142857143, -14.5714285714286, 
-15.1428571428571, -15.7142857142857, -16.5714285714286, -16.7142857142857, 
-16.5714285714286, -18, -19.5714285714286, -24.7142857142857, 
-19.8571428571429, -20, -19.5714285714286, -22.4285714285714, 
-22.2857142857143, -23, -24.8571428571429, -25.1428571428571, 
-26)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame"
))

fig <- plot_ly(w, x = ~Date, y = ~`Actual-retail and recreation`,text="Information", name = 'Reported VMT', type = 'scatter', mode = 'lines',
               line = list(color = 'rgb(205, 12, 24)', width = 4),hovertemplate = paste(
                 "<b>%{text}</b><br><br>",
                 '<i><b>Date</b></i>: %{x}',
                 '<br><b>Value</b>: %{y}<br>',
                 '<extra></extra>'
               ))
fig

解决方案

只需在Plotly的layout中配置X轴的刻度规则,指定按周显示即可。修改后的完整代码如下:

w<-structure(list(Date = structure(c(1582329600, 1582934400, 1583539200, 
1584144000, 1584748800, 1585353600, 1585958400, 1586563200, 1587168000, 
1587772800, 1588377600, 1588982400, 1589587200, 1590192000, 1590796800, 
1591401600, 1592006400, 1592611200, 1593216000, 1593820800, 1594425600, 
1595030400, 1595635200, 1596240000, 1596844800, 1597449600, 1598054400, 
1598659200, 1599264000, 1599868800, 1600473600, 1601078400, 1601683200, 
1602288000, 1602892800, 1603497600, 1604102400, 1604707200, 1605312000, 
1605916800, 1606521600, 1607126400, 1607731200, 1608336000, 1608940800, 
1609545600, 1610150400, 1610755200, 1611360000, 1611964800), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), `Actual-retail and recreation` = c(3.57142857142857, 
6.85714285714286, 8.85714285714286, 4.57142857142857, -22.7142857142857, 
-41.5714285714286, -42, -43.7142857142857, -44.2857142857143, 
-40.1428571428571, -35, -30.1428571428571, -28.8571428571429, 
-25, -23.4285714285714, -19.1428571428571, -16.4285714285714, 
-14.2857142857143, -15.2857142857143, -13.7142857142857, -15.7142857142857, 
-14.7142857142857, -15.2857142857143, -14.5714285714286, -13.8571428571429, 
-14.7142857142857, -15.1428571428571, -15.8571428571429, -13.4285714285714, 
-15.7142857142857, -15, -15.2857142857143, -14.5714285714286, 
-15.1428571428571, -15.7142857142857, -16.5714285714286, -16.7142857142857, 
-16.5714285714286, -18, -19.5714285714286, -24.7142857142857, 
-19.8571428571429, -20, -19.5714285714286, -22.4285714285714, 
-22.2857142857143, -23, -24.8571428571429, -25.1428571428571, 
-26)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame"
))

fig <- plot_ly(w, x = ~Date, y = ~`Actual-retail and recreation`,text="Information", name = 'Reported VMT', type = 'scatter', mode = 'lines',
               line = list(color = 'rgb(205, 12, 24)', width = 4),hovertemplate = paste(
                 "<b>%{text}</b><br><br>",
                 '<i><b>Date</b></i>: %{x}',
                 '<br><b>Value</b>: %{y}<br>',
                 '<extra></extra>'
               )) %>%
  layout(
    xaxis = list(
      tickformat = "%Y-W%U",  # 显示格式为「年份-W周数」,如2020-W07
      dtick = 7*24*60*60*1000, # 刻度间隔为一周(毫秒单位)
      tickangle = 45, # 可选:旋转标签避免重叠
      tickmode = "linear" # 确保刻度按固定间隔生成
    )
  )
fig

关键参数说明

  • tickformat: 定义X轴标签的显示格式,%Y-W%U是最直观的周格式;也可以用%Y-%U显示为「2020-07」
  • dtick: 设置刻度的时间间隔,7*24*60*60*1000等于一周的毫秒数,保证每个刻度对应一周
  • tickangle: 当标签较多时,旋转45度可以避免文字重叠,提升可读性

内容的提问来源于stack exchange,提问作者firmo23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 02:30:06