Search code examples
rrstudiodygraphsas.date

Dygraphs error in as.POSIXlt.character


Trying to create a graph with the dygraphs package. I've converted the dates using as.Date() but I'm getting this message:

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

The first few rows of my data looks like this:

          Date   Depth      Fecal.Coliform Dissolved.Oxygen
1   1992-05-02   0.5            6.0               NA
2   1992-05-15   0.5             NA         9.900000
3   1992-05-31   0.5             NA         8.800000
4   1992-06-11   0.5             NA         8.900000

Solution

  • It seems dygraphs likes timeseries data more than dates. Try to convert your dates using xts:

    library(xts)
    library(dygraphs)
    
    data <- data.frame(Date  = as.Date(c("1992-05-02", "1992-05-15",
                                       "1992-05-31", "1992-06-11")),
                       Depth = c(0.5, 0.5, 0.5, 0.5),
              Fecal.Coliform = c(6.0, NA, NA, NA),
            Dissolved.Oxygen = c(NA, 9.900000, 8.800000, 8.900000))
    
    # This produces your error
    dygraph(data)
    
    # convert your data to time series and plot it!
    new.data <- cbind(xts(data$Dissolved.Oxygen, data$Date),
                          data$Fecal.Coliform, data$Depth)
    colnames(new.data) <- c("Dissolved.Oxigen", "Fecal.Coliform", "Depth")
    dygraph(new.data)