簡體   English   中英

Plotly R中的時間序列

[英]Time series in Plotly R

所以,我有這個數據

test_data <- structure(list(
  time = c(29510, 29528.023023023, 29546.046046046, 
           29564.0690690691, 29582.0920920921, 29600.1151151151, 
           29618.1381381381, 29636.1611611612, 29654.1841841842, 
           29672.2072072072, 29690.2302302302, 29708.2532532533, 
           29726.2762762763, 29744.2992992993, 29762.3223223223, 
           29780.3453453453, 29798.3683683684, 29816.3913913914, 
           29834.4144144144, 29852.4374374374), 
  sum = c(0L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 5L, 7L, 9L, 9L, 15L, 
          17L, 18L, 18L, 18L, 18L, 21L)), 
  .Names = c("time", "sum"), 
  row.names = c(NA, 20L), 
  class = "data.frame")

時間是數字。

我可以使用ggplot2完美地繪制它:

ggplot(test_data) +
    geom_line(aes(x=time, y=sum)) + 
    scale_x_time()

但是,當我嘗試使用plot_ly ,時間錯誤地顯示為數字而不是時間。 在某種程度上類似於ggplot2的scale_x_time函數嗎?

plot_ly(test_data, x = ~time, y = ~sum, type = 'scatter', mode = 'lines')

怎么樣,整合@ Z.Lin的建議,並消除一天的必要性:

test_data$time <- as.POSIXct(test_data$time, origin = "1970-01-01")  # as date
test_data$time <- strftime(test_data$time,format="%H:%M:%S")         # remove the day
plot_ly(test_data, x = ~time, y = ~sum, type = 'scatter', mode = 'lines')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM