rCharts中的Highcharts区域范围图

时间:2013-11-25 13:48:42

标签: r highcharts rcharts

有没有人知道是否可以在rCharts(http://www.highcharts.com/demo/arearange-line)中创建区域范围样式图?

我的基本问题是简单预测方案的可视化:

set.seed(123134)
y <- c(rnorm(20, 35, 2), rep(NA, 10))
data <- data.frame(y=y)
data$fc <- c(rep(NA, 20), rnorm(10, 35, 1))
data$lci <- data$fc-1
data$uci <- data$fc+1

h1 <- Highcharts$new()
h1$chart(type="line")
h1$series(data=data$y, marker = list(symbol = 'circle'), connectNulls = TRUE)
h1$series(data=data$fc, marker = list(symbol = 'circle'), connectNulls = TRUE)
h1$series(data=data$uci, showInLegend = FALSE, marker = list(symbol = 'square'), connectNulls = TRUE)
h1$series(data=data$lci, showInLegend = FALSE, marker = list(symbol = 'square'), connectNulls = TRUE)
h1$series(data=rep(30,30), marker= list(enabled = FALSE))
h1

谢谢!

1 个答案:

答案 0 :(得分:4)

这是example of an area range chart。我也在下面添加代码。

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。输入系列的更简单方法是使用series方法并分别添加两个数据系列。这避免了嵌套列表,这可能会变得丑陋。

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:要添加范围描述,如在高价图表中,您可以添加以下代码行。请注意,set方法可用于为图表添加任意键值对。

h1$set(tooltip = list(
  crosshairs =  T,
  shared = T,
  valueSuffix =  '°C'
))