1

I'm trying to plot some time-series data in R using the dygraphs package (I'm an R newbie), but having trouble and I suspect it's due to data format issues.

Here is a sample of my code:

df <- select(sal_data,DateTime,'Sal psu')
head(df)
typeof(df)
class(df)

outputs:

DateTime
<S3: POSIXct>
Sal psu
<dbl>
2018-05-03 14:30:01 24.93           
2018-05-03 14:45:01 24.94           
2018-05-03 15:00:01 24.90           
2018-05-03 15:15:01 24.89           
2018-05-03 15:30:01 24.94           
2018-05-03 15:45:01 24.87   

and

[1] "list"
[1] "tbl_df"     "tbl"        "data.frame"

but when I try to do dygraph(as.xts(df)) I get:

Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format

Any thoughts? This seems like a simple fix but my searching hasn't found anything. Thanks!

cerkiewny
  • 2,761
  • 18
  • 36
DarwinsBeard
  • 527
  • 4
  • 16

1 Answers1

0

You are not converting the df into a proper xts object:

library(dygraphs)
library(dplyr)

myData <- data.frame(time=c("2014-01-23 14:28:21","2014-01-23 14:28:55",
                            "2014-01-23 14:29:02","2014-01-23 14:31:18"),
                     speed=c(2.0,2.2,3.4,5.5)) %>%
  mutate(time = as.POSIXct(strptime(time,"%Y-%m-%d %H:%M:%S")))

myData_tbl <- tibble::as.tibble(myData)
dygraph(xts::xts(myData_tbl, order.by=myData_tbl$time))

yieldsenter image description here

Stedy
  • 7,359
  • 14
  • 57
  • 77