R 即使没有该间隙的记录,也要使图形线中断

R 即使没有该间隙的记录,也要使图形线中断,r,time-series,plotly,r-plotly,R,Time Series,Plotly,R Plotly,我正面临一个问题,用plotly在R中绘制时间序列。 基本上,我将数据放在如下结构的数据框中: datetime | measure 2018-04-09 00:40:05 | 3.4 2018-04-09 00:41:00 | 3.9 2018-04-09 00:42:07 | 3.8 2018-04-09 00:43:00 | 3.7 2018-04-09 00:44:00 | 4.2 2018-04-09 00:45:00 | 5.8 2018-04-09 01:15

我正面临一个问题,用plotly在R中绘制时间序列。 基本上,我将数据放在如下结构的数据框中:

datetime            | measure
2018-04-09 00:40:05 | 3.4
2018-04-09 00:41:00 | 3.9
2018-04-09 00:42:07 | 3.8
2018-04-09 00:43:00 | 3.7
2018-04-09 00:44:00 | 4.2
2018-04-09 00:45:00 | 5.8
2018-04-09 01:15:00 | 5.8
观察的间隔是不均匀的,我每分钟有一次观察,但只是大致上,可能有几秒钟的差异。 然后我在观察之间有更大的差距,在我在这里放的例子中,上一次观察和上一次观察之间有30分钟的差距


Plotly显然是在连接折线图中的最后两个观察值,而我想要的是它们之间的空白。

您可以通过将数据划分为“块”并将每个块绘制为单独的轨迹来实现这一点。可复制示例:

df <- data.frame(
  time = Sys.time() - c(1:10, 51:60),
  value = runif(20),
  chunk = rep(c("A", "B"), each = 10)
)

p <- plot_ly(data = df[which(df$chunk == "A"),], x = ~time, y = ~value, name = "chunk A", type = "scatter", mode = "lines") %>%
  add_trace(data = df[which(df$chunk == "B"),], x = ~time, y = ~value, name = "chunk B", type = "scatter", mode = "lines")

# or if there are more 'chunks'
p2 <- plot_ly(data = df, x = ~time, y = ~value, color = ~chunk, type = "scatter", mode = "lines")

df尝试使用
connectgaps=False
;检查