Atius
Atius

Reputation: 191

Time series in Plotly R

So, I have this data

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")

Where the time is numeric.

I can plot it perfectly with ggplot2 using this:

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

But when I try to use plot_ly the time comes out incorrectly as numbers instead of time. Is there something in plotly similar to ggplot2's scale_x_time function?

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

Upvotes: 0

Views: 97

Answers (1)

s__
s__

Reputation: 9525

What about this, integrating the @Z.Lin's advice, and removing the necessity of the day:

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')

Upvotes: 1

Related Questions