Search code examples
rxtsdygraphs

Using dygraph with xts-object drops Label in plot


I have a seemingly small challenge, but I can't get to an answer. Here is my minimum working example.

fr_nuke <- structure(list(Date = structure(c(1420070400, 1420074000, 1420077600,
  1420081200, 1420084800, 1420088400), class = c("POSIXct", "POSIXt"), tzone = ""),
  `61` = c(57945, 57652, 57583, 57551, 57465, 57683),
  `3244` = c(72666.64, 73508.78, 69749.17, 67080.13, 66357.65, 66524.13),
  `778` = c(2.1133, 2.1133, 2.1133, 2.1133, 2.1133, 2.1133),
  fcasted_nuke_temp = c(54064.6099092888, 54064.6099092888, 54064.6099092888,
  54064.6099092888, 54064.6099092888, 54064.6099092888),
  fcasted_nuke_cons = c(55921.043096775, 56319.5688170977, 54540.4094334057,
  53277.340242333, 52935.4411965463, 53014.2244890147)),
  .Names = c("Date", "61", "3244", "778", "fcasted_nuke_temp", "fcasted_nuke_cons"),
  row.names = c(NA, 6L), class = "data.frame")

series1 <- as.xts(fr_nuke$'61', fr_nuke$Date)
series2 <- as.xts(fr_nuke$fcasted_nuke_temp, fr_nuke$Date)
series3 <- as.xts(fr_nuke$fcasted_nuke_cons, fr_nuke$Date)
grp_input <- cbind(series1,series2,series3)

dygraph(grp_input)

The resulting plot does not show the label of the individual series. Specifying the series with

dygraph(grp_input) %>% dySeries("V1", label = "Label1")

Results in:

Error in dySeries(., "V1", label = "Label1") : One or more of the specified series were not found. Valid series names are: ..1, ..2, ..3

However, it works if I plot only one series (e.g. series1).

dygraph(series1) %>% dySeries("V1", label = "Label1") 

Solution

  • Either set the colnames for the grp_input object, or use merge to construct the column names from the object names.

    # setting colnames
    require(dygraphs)
    require(xts)
    grp_input <- cbind(series1, series2, series3)
    colnames(grp_input) <- c("V1", "V2", "V3")
    dygraph(grp_input) %>% dySeries("V1", label = "Label1")
    
    # using merge
    require(dygraphs)
    require(xts)
    grp_input <- merge(series1, series2, series3)
    dygraph(grp_input) %>% dySeries("series1", label = "Label1")