Skip to content

Instantly share code, notes, and snippets.

@corynissen
Created May 23, 2014 20:22
Show Gist options
  • Save corynissen/1d1bd39e906f6ebdef34 to your computer and use it in GitHub Desktop.
Save corynissen/1d1bd39e906f6ebdef34 to your computer and use it in GitHub Desktop.
trying dual axis scatterplots with dates...
cs <-c("13-10-30 12:30:00", "13-10-30 12:35:00", "13-10-30 12:40:00",
"13-10-30 12:45:00", "13-10-30 12:50:00", "13-10-30 12:55:00")
x <-strptime(cs, "%y-%m-%d %H:%M:%S")
dfr <-data.frame(date=as.POSIXct(x,origin="1970-01-01"),
value1=c(1.5,1.25,.75,2.1,1.3,1.4),
value2=c(3.5,3.25,1.75,.1,6.3,2.4))
h1 <- Highcharts$new()
h1$chart(type = "scatter")
h1$chart(zoomType = "xy")
h1$xAxis(type = "datetime", data=as.POSIXct(dfr$date,origin="1970-01-01"))
h1$series(name = 'Temperature', type = 'scatter', color = '#000099',
data = dfr$value1)
h1$series(name = 'Humidity', type = 'scatter', color = '#FF0000',
data = dfr$value2, yAxis=1)
h1$yAxis(list(list(title = list(text = 'Temperature'), opposite = FALSE),
list(title = list(text = 'humidity_rh'), opposite = TRUE)))
@kohske
Copy link

kohske commented May 24, 2014

Here is, at least for the first issue.
the second one is easy.

cs <-c("13-10-30 12:30:00", "13-10-30 12:35:00", "13-10-30 12:40:00", 
       "13-10-30 12:45:00", "13-10-30 12:50:00", "13-10-30 12:55:00")
x <-strptime(cs, "%y-%m-%d %H:%M:%S")
dfr <-data.frame(date=1000*as.numeric(as.POSIXct(x,origin="1970-01-01", tz="UTC")), 
                 value1=c(1.5,1.25,.75,2.1,1.3,1.4),
                 value2=c(3.5,3.25,1.75,.1,6.3,2.4))
h1 <- Highcharts$new()
h1$chart(type = "scatter")
h1$chart(zoomType = "xy")
h1$xAxis(type = "datetime")
h1$series(name = 'Temperature', type = 'scatter', color = '#000099',
          data = toJSONArray2(dfr[c(1, 2)], json = F, name = F))
h1$series(name = 'Humidity', type = 'scatter', color = '#FF0000',
          data = toJSONArray2(dfr[c(1, 3)], json = F, name = F), yAxis=1)
h1$yAxis(list(list(title = list(text = 'Temperature'), opposite = FALSE), 
              list(title = list(text = 'humidity_rh'), opposite = TRUE)))
h1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment