Highcharts Area Range Plot in rCharts

与世无争的帅哥 提交于 2019-12-06 03:56:39

Here is an example of an area range chart. I am adding the code below as well.

library(rCharts)
# year is converted to highcharts compatible datetime format
huron <- data.frame(
  year = as.numeric(as.POSIXct(paste0(1875:1972, '-01-01')))*1000, 
  level = as.vector(LakeHuron)
)

# add ymin and ymax to data
dat <- transform(huron,
  ymin = level - 1,
  ymax = level + 1
)

# initialize highcharts object
# add each layer as a series
h1 <- Highcharts$new()
h1$series(list(
  list(
    data = toJSONArray2(dat[,c('year', 'level')], names = F, json = F),
    zIndex = 1
  ),
  list(
    data = toJSONArray2(dat[,c('year', 'ymin', 'ymax')], names = F, json = F),
    type = 'arearange',
    fillOpacity = 0.3,
    lineWidth = 0,
    color = 'lightblue',
    zIndex = 0
  )
))
h1$xAxis(type = 'datetime')
h1

EDIT. A simpler way to input series is to use the series method and add the two data series separately. This avoids the nested list, which can get ugly.

h1 <- Highcharts$new()
h1$series(
  data = toJSONArray2(dat[,c('year', 'level')], names = F, json = F),
  zIndex = 1,
  name = "Level"
)
h1$series(
  data = toJSONArray2(dat[,c('year', 'ymin', 'ymax')], names = F, json = F),
  type = 'arearange',
  fillOpacity = 0.3,
  lineWidth = 0,
  color = 'lightblue',
  zIndex = 0
)
h1$xAxis(type = 'datetime')
h1

EDIT2: To add a range description as in the highcharts chart, you can add the following line of code. Note that the set method can be used to add any arbitrary key-value pair for a chart.

h1$set(tooltip = list(
  crosshairs =  T,
  shared = T,
  valueSuffix =  '°C'
))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!