R Error: Unsupported type passed to argument 'data'

Advertisements

I am working with the R programming language.

I am following the tutorial here https://rstudio.github.io/dygraphs/ to make the following graph:

I tried to simulate some data for this example:

library(dygraphs)


set.seed(1)
date <- seq(as.Date("2010-01-01"), as.Date("2010-12-01"), by = "month")
var1 <- rnorm(length(date), mean = 100, sd = 10)
var2 <- rnorm(length(date), mean = 50, sd = 5)
df <- data.frame(date, var1, var2)

Then, I tried to make this graph:

dygraph(df, main = "Stacked Graph") %>%
  dySeries("var1", label = "Var1") %>%
  dySeries("var2", label = "Var2") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)

But I get the following error:

Error in dygraph(df, main = "Stacked Graph") : 
  Unsupported type passed to argument 'data'.

Can someone please show me how to fix this?

Thanks!

Update: I think I was able to figure it out:

library(xts)
df_xts <- xts(df[,-1], order.by = df$date)

dygraph(df_xts, main = "Stacked Graph") %>%
  dySeries("var1", label = "Var1") %>%
  dySeries("var2", label = "Var2") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)

>Solution :

In the linked example, lungDeaths is a time series object, not a data frame. You can create df as follows:

var1 <- ts(var1, start = 2010, frequency = 12)
var2 <- ts(var2, start = 2010, frequency = 12)
df <- cbind(var1, var2)

Then your plotting code produces

Leave a ReplyCancel reply