Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I scale points in Dygraphs?

I am using the great dygraphs package for R (https://rstudio.github.io/dygraphs/)

My code as of right now is:

james<-mtcars[c("mpg","drat")]
james$date<-seq(from=as.Date("2013-05-16"),to=as.Date("2013-06-16"),by="days")
x <- xts::xts(james$mpg, order.by = james$date)
p <- dygraphs::dygraph(x, main = "mpg over time", xlab = "Date", ylab = "mpg") %>%
     dygraphs::dyRangeSelector() %>% 
     dyOptions(drawPoints = TRUE, pointSize = 2)
p

I want to scale the size of points in p by james$drat, rather than having it fixed at 2.

How can I do this?

like image 312
Jimbo Avatar asked Jul 08 '15 17:07

Jimbo


1 Answers

I know this is an old question, but I have found a potential solution using drawPointCallback. I hope it helps someone out there. You could choose a scale either in R or JavaScript if you did not want to use the raw number.

library(dygraphs)

james<-mtcars[c("mpg","drat")]
james$date<-seq(from=as.Date("2013-05-16"),to=as.Date("2013-06-16"),by="days")
x <- xts::xts(james$mpg, order.by = james$date)
p <- dygraphs::dygraph(x, main = "mpg over time", xlab = "Date", ylab = "mpg") %>%
  dygraphs::dyRangeSelector() %>% 
  dyOptions(drawPoints = TRUE)
p %>%
  dyCallbacks("drawPointCallback" = sprintf(
    "
function(g, name, ctx, canvasx, canvasy, color, radius, index) {
  var drat = %s;
  radius = drat[index];
  return Dygraph.Circles.DEFAULT(g, name, ctx, canvasx, canvasy, color, radius)
}
",
    jsonlite::toJSON(mtcars$drat)
    )
  )
like image 96
timelyportfolio Avatar answered Sep 18 '22 15:09

timelyportfolio