【问题标题】:First and last observation being cut in half when using `layout.xaxis.range`使用`layout.xaxis.range`时,第一个和最后一个观察被减半
【发布时间】:2020-10-04 04:00:17
【问题描述】:

在使用layout.xaxis.range 时,如何避免绘制的第一个和最后一个观察结果被切成两半?

当使用标准的plotly example 修改为烛台图(分别为 2015 年 12 月 1 日和 2015 年 1 月 15 日的第一根和最后一根蜡烛)时,这一点变得非常明显:

library(plotly)

df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig <- plot_ly(
  type = "candlestick",
  x     = ~df$Date,
  open  = ~df$AAPL.Open,
  high  = ~df$AAPL.High,
  low   = ~df$AAPL.Low,
  close = ~df$AAPL.Close
)
fig <- fig %>%
  layout(
    title = "Time Series with Custom Date-Time Format",
    xaxis = list(
      type = "date",
      range = c('2015-12-01', '2016-01-15')
    )
  )
fig

【问题讨论】:

  • 你有图片说明你的意思吗?图表上是否没有足够的空间来适应图形的宽度?偏移或稍微扩大范围会有帮助吗?
  • 嗯,在使用 lubridate 时有一种方法。但是,没有更优雅的方法吗? range = c(as.character(as.Date('2015-12-01') - hours(12)), as.character(as.Date('2016-01-15') + hours(12)))
  • 这种行为是预期的,因为烛台是在点 (x,y) 绘制的,并且一半的烛台将占据一半的水平空间。您可以首先使用“正确”数据或将范围设置为 (x-1, x+1) 来避免这种情况,如上面的评论中所述。
  • 正确,x - 0.5 和 x + 0.5 处理逐项观察或x - hours(12) 和 x - hours(12) 分别处理 x 轴左侧和右侧的日期。然而,一个优选的解决方案(也关于渲染时间 - 下面由@user2554330 提供的答案/评论部分)将图表区域从图表区域周围的框架中偏移(与标准 R 图一样),从而提供空间为完整的蜡烛。

标签: javascript r plotly r-plotly plotly.js


【解决方案1】:

不要在绘图中指定范围,而是获取数据的一个子集并使用默认范围绘制它。例如,

df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df <- subset(df, Date >= "2015-12-01" & Date <= "2016-01-15")

fig <- plot_ly(
  type = "candlestick",
  x     = ~df$Date,
  open  = ~df$AAPL.Open,
  high  = ~df$AAPL.High,
  low   = ~df$AAPL.Low,
  close = ~df$AAPL.Close
)
fig <- fig %>%
  layout(
    title = "Time Series with Custom Date-Time Format",
    xaxis = list(
      type = "date"
    )
  )
fig

请注意,这是基于字符的日期比较;由于您的数据采用 YYYY-MM-DD 格式,因此这是可行的,但通常在子集之前转换为 Date 类型会更安全。

【讨论】:

  • 没错,但是,当使用 microbenchmark 包进行比较时,fig &lt;- ... 的第一个定义(渲染整个图)大约需要 1 毫秒,而第二个(仅显示范围)需要 0.1 毫秒。
  • @user2554330 你的 sn-p 只生成一个空白图表。
  • @vestland:也许您的系统以不同的格式下载日期,或者出现故障而您根本没有下载任何东西。
  • @user2554330 我不这么认为。问题中的 sn-p 工作得很好。
  • user2554330 提供的 sn-p 在我这边也可以正常工作...
【解决方案2】:

第一个问题似乎与您的日期未采用预期格式(另存为文本)有关。解决这个问题应该避免 x 范围被中途切断。

library(plotly)

df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df$Date <- as.POSIXct(df$Date)  # Instead of keeping the date as character

fig <- plot_ly(
  type = "candlestick",
  x     = ~df$Date,
  open  = ~df$AAPL.Open,
  high  = ~df$AAPL.High,
  low   = ~df$AAPL.Low,
  close = ~df$AAPL.Close
)
fig <- fig %>%
  layout(
    title = "Time Series with Custom Date-Time Format"
  )
fig

对于您与范围相关的需求,我会看看range slider 和range selector buttons 配置。

【讨论】:

  • 提供的答案绘制了整个时间序列,因此在图表的左侧和右侧提供了完整的蜡烛。使用范围滑块或代码第二部分 (fig &lt;- fig %&gt;% ...) 中的 xaxis = list(type = "date", range = c('2015-12-01', '2016-01-15')) 更改范围时,即使更改为 POSIXct,问题也会再次出现。
  • 此外,关于整个绘图的渲染时间与仅显示子周期(请参阅@user255430 的回答/评论部分),首选的解决方案是将图表区域从周围的框架中偏移图表区域(类似于标准 R 图),从而为完整的蜡烛提供空间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
相关资源
最近更新 更多