Share the Gallery !Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

This page is dedicated to the basic utilisation of the dygraphs library. This library allows to represent time series: a line plot where the X axis represent time, and the Y axis the evolution of one or several variables. The Dygraphs library allows to make interactive charts: you can zoom on a specific time period, hover a data point to have more info, and more. Two input format are possible for the time: just a numeric count ( 1,2,3,4,5..) ,or a real date format ( 2017/12/08 )

 

1- Time is a numeric value


In this case the utilisation of the Dygraphs library is really easy. You just need to make in sort that the first column of your dataset is the numeric variable that describes the time. See code and graphic below:

 

 

2- Time is a date


The process is a bit more complicated if your time axis is a real date. By real date, I mean a format recognised as a Date by R. You can test that by doing str(data). This will give you the format of each column, so check that ‘Date’ is written. If it is the case, you can transform your data to the its format (xts=eXtensible Time Series) that is requested by dygraphs. See the code below:

 

 

3- Several variables


The process to plot several variables is very close. When you do the transformation to the its format, simply specify all the columns you want to keep on your chart

 

 

4- Getting the date format


The hardest part in this process is to get your data at the date format! This can be a real struggle, and that is why I highly recommend to use the lubridate library and read this document. Most of the time you have your data at text format (like from excel spreadsheet). When you load these data you get a character format. You want to transform it in a date, time or date-time format. A set of functions with relevant names exist for that. Here is an example of utilisation on a real dataset:

 

 

 

As a memo, here is an overview of the function that exist:

ymd(“20110604”)

mdy(“06-04-2011”)

dmy(“04/06/2011”)

ymd_hms(“2011-06-04 12:00:00”)

 

 

Related


 

Search the gallery



Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz