dygraphs for generic xy data

97 views
Skip to first unread message

Jonathan

unread,
Aug 29, 2015, 3:35:49 PM8/29/15
to Shiny - Web Framework for R
Hi,

The interactivity tools (x-zoom, y-zoom, pan, and context highlighting) of dygraphs is outstanding. But I'd like to use it for more general x-y data.

For example, how may I 'trick' it to plot x vs y displayed as points of the following vectors? And display numeric x axis labels including the negative numbers?

x = rnorm(10)
y = rnorm(10)

Jonathan

Jonathan

unread,
Aug 29, 2015, 5:17:17 PM8/29/15
to Shiny - Web Framework for R
There are two aspects: 1) working with decimal numbers and 2) working with negative numbers. I can work with both by doing something like:

library(dygraphs)
x = rnorm(10)
y = rnorm(10)
options(digits.secs = 3)
dg=xts(y,Sys.time()+x)
dygraph(dg)

But the resulting plot rounds the fractional x values to whole seconds. I found the following two links to show how to handle milliseconds.


I read carefully both posts and tried to create my own version below. I can not get it to work. My browser shows a blank page, and the java console shows the message "Uncaught TypeError: d.getHours is not a function".

Please help me get this to a working example. Thanks.

library(dygraphs)
CustomAxisLabel <- 'function (d, gran) {
                              return Dygraph.zeropad(d.getHours()) + ":" + 
                                     Dygraph.zeropad(d.getMinutes()) + ":" + 
                                     Dygraph.zeropad(d.getSeconds()) + "." + 
                                     Dygraph.zeropad(d.getMilliseconds());
                             }'
CustomValueFormat = 'function (ms) {
                          var d = new Date(ms);
                          return Dygraph.zeropad(d.getHours()) + ":" + 
                          Dygraph.zeropad(d.getMinutes()) + ":" + 
                          Dygraph.zeropad(d.getSeconds()) + "." + 
                          Dygraph.zeropad(d.getMilliseconds());
                         }'
len <- 60 * 60  + 10
RDData <- xts(runif(len,0,1), seq(as.POSIXct("2015-04-01 10:00:00"),  as.POSIXct("2015-04-02 15:00:00"), length = len),tz="GMT")
dygraph(RDData) %>%  dyOptions(useDataTimezone = TRUE) %>% dyAxis("x",axisLabelFormatter =JS(CustomAxisLabel),valueFormatter=JS(CustomValueFormat)) 
Reply all
Reply to author
Forward
0 new messages