2014-10-22 80 views
2

我正在嘗試使用rCharts的繪圖接口創建一個時間序列圖到Highcharts庫。 我想弄清楚如何根據其y值設置單個點的顏色。我發現一種方法可以爲線條和點提供不同的顏色,但只能作爲一組,而不是單獨的數據點。rCharts:更改時間序列圖的單個點顏色(Highcharts)

下面是測試代碼:

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的原因是,它繪製時間序列數據很好,它有也很酷放大功能。

在此先感謝您的幫助&的建議。 揚

回答

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

相關問題