rCharts:更改时间序列图的各个点颜色(Highcharts)

时间:2014-10-22 12:20:56

标签: r highcharts rcharts

我正在尝试使用rCharts的绘图界面创建一个时间序列图到Highcharts库。 我试图找出如何根据其y值设置单个点的颜色。我找到了一种方法,可以为线条和点设置不同的颜色,但只能作为一个组,而不是单独的数据点。

这是测试代码:

library(rCharts)
library(rjson)
TransformDate <- function(x){
  as.numeric(as.POSIXct(x, origin="1970-01-01")) * 1000
}

x <- TransformDate(c('2013-01-01 11:05:35', '2013-03-03 04:50:35', '2013-05-05 21:09:37', '2013-07-07 12:49:05'))
y <- c(1,56,123,1000)

w<-TransformDate(c('2013-01-10 11:05:35', '2013-03-13 04:50:35', '2013-05-15 21:09:37', '2013-07-17 12:49:05'))
z<-c(10, 100, 70, 500)

df1 <- data.frame(x = x,y = y)
df2 <- data.frame(x = w, y = z)
combo <- rCharts:::Highcharts$new()
combo$series(list(list(data = rCharts::toJSONArray2(df1, json = F, names = F), name = "Temp1", marker = list(fillColor = c('#999'), lineWidth=6, lineColor=c('#999'))),
              list(data = rCharts::toJSONArray2(df2, json = F, names = F), name = "Temp2")))


combo$xAxis(type='datetime')
combo$chart(type = "scatter")
combo$chart(zoomType="x")

combo

我相信这可以在Polycharts中完成,但我使用highcharts的原因是它很好地绘制了时间序列数据,并且还具有很酷的放大功能。

提前感谢您的帮助&amp;建议。 扬

1 个答案:

答案 0 :(得分:0)

这是分别控制线条/标记的颜色/尺寸的一种方法:

h <- rCharts:::Highcharts$new()
h$series(list(
  list(data = rCharts::toJSONArray2(df1, json = FALSE, names = FALSE), 
      name = "Big Reds",
      color = '#FF0000',
      lineWidth = 4,
      marker = list(
        fillColor = '#FFA500', 
        radius = 10)
      ),
  list(data = rCharts::toJSONArray2(df2, json = FALSE, names = FALSE), 
      name = "Small Blues", 
      color = '#0000FF',
      lineWidth = 2,
      marker = list(
        fillColor = '#ADD8E6',
        radius = 6)
)))
h$xAxis(type = 'datetime')
h$chart(type = "scatter")
h$chart(zoomType = "x")
h

enter image description here