Peter Chen
Peter Chen

Reputation: 1484

R- using dygraph with csv

following is my ex.csv data input to R.

      Date            pr          pa 
1  2015-01-01     6497985     4833118  
2  2015-02-01       88289     4305786  
3  2015-03-01           0     1149480  
4  2015-04-01           0    16706470  
5  2015-05-01           0     7025197  
6  2015-06-01           0     6752085  

also, here is raw data

Date,pr,pa
2015/1/1,6497985,4833118
2015/2/1,88289,4305786
2015/3/1,0,1149480
2015/4/1,0,16706470
2015/5/1,0,7025197
2015/6/1,0,6752085

how can I use R package dygraph with this data?

> str(ex) 
'data.frame':   6 obs. of  3 variables:
 $ Date: Factor w/ 6 levels "2015/1/1","2015/2/1",..: 1 2 3 4 5 6
 $ pr  : int  6497985 88289 0 0 0 0
 $ pa  : int  4833118 4305786 1149480 16706470 7025197 6752085

> dygraph(ex) 
Error in dygraph(ex) : Unsupported type passed to argument 'data'. 

Please help me.appreciate a lot.

Upvotes: 0

Views: 913

Answers (1)

Buggy
Buggy

Reputation: 2100

Here are the steps to get it done: First, you need to convert your strings to a Date that is understandable for R. Then convert your data to an xts time series (required by dygraphs). Then plot it with dygraphs.

library(dygraphs)
library(xts)
data<-read.csv("test.csv")
data$Date<- as.Date(data$Date) #convert to date

time_series <- xts(data, order.by = data$Date) #make xts
dygraph(time_series) #now plot

Upvotes: 2

Related Questions